-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
457 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,4 @@ | |
"is-ci": "^3.0.0" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/plugins/plugin-build/src/commands/bundle/ignore/const.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Filename } from "@yarnpkg/fslib"; | ||
|
||
export const DEFAULT_IGNORE_FILE = ".bundleignore" as Filename; |
20 changes: 20 additions & 0 deletions
20
packages/plugins/plugin-build/src/commands/bundle/ignore/getAllFiles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Filename } from "@yarnpkg/fslib"; | ||
import glob from "glob-promise"; | ||
|
||
interface GetAllFilesProps { | ||
cwd: string; | ||
} | ||
|
||
export const getAllFiles = async ({ | ||
cwd, | ||
}: GetAllFilesProps): Promise<Filename[]> => { | ||
try { | ||
const files = (await glob(`${cwd}/**/*`, { dot: true })) as Filename[]; | ||
|
||
return files | ||
.map((fileName) => fileName.split(`${cwd}/`)[1] ?? "") | ||
.filter(Boolean) as Filename[]; | ||
} catch (_e) { | ||
return []; | ||
} | ||
}; |
70 changes: 70 additions & 0 deletions
70
packages/plugins/plugin-build/src/commands/bundle/ignore/getAllWorkspacesNonRemovables.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Workspace } from "@yarnpkg/core"; | ||
import { PackageFiles } from "../../../types"; | ||
import { join, dirname } from "path"; | ||
|
||
const NonRemovableFiles: Record< | ||
string, | ||
(props: { cwd: string; rootDir: string }) => string[] | ||
> = { | ||
/* | ||
Make sure the directory for package is not removed | ||
*/ | ||
directory: ({ cwd }) => [cwd], | ||
/* | ||
Make sure that parent directories are not removed, for example a workspace with path: | ||
/extrafolder/packages/backend | ||
both /extrafolder | ||
and /extrafolder/package are kept safe | ||
*/ | ||
parentDirectories: ({ cwd, rootDir }) => { | ||
if (!cwd.startsWith(rootDir)) { | ||
throw new Error( | ||
"Package directory not in rootDir. This should never happen" | ||
); | ||
} | ||
let currentPath = cwd; | ||
let paths: string[] = []; | ||
|
||
while (true) { | ||
if (currentPath === rootDir || currentPath.length < rootDir.length) { | ||
return paths; | ||
} | ||
currentPath = dirname(currentPath); | ||
paths = [...paths, currentPath]; | ||
} | ||
}, | ||
/* Make sure that package files are kept */ | ||
packageFiles: ({ cwd }) => | ||
PackageFiles.map((fileName) => join(cwd, fileName)), | ||
}; | ||
|
||
const getAllWorkspacesNonRemovablesHelper = ({ | ||
cwd, | ||
rootDir, | ||
}: { | ||
cwd: string; | ||
rootDir: string; | ||
}) => { | ||
return [ | ||
...new Set( | ||
...[ | ||
Object.values(NonRemovableFiles) | ||
.map((fn) => fn({ cwd, rootDir })) | ||
.flat(), | ||
] | ||
), | ||
]; | ||
}; | ||
|
||
export const getAllWorkspacesNonRemovables = ({ | ||
workspaces, | ||
rootDir, | ||
}: { | ||
workspaces: Workspace[]; | ||
rootDir: string; | ||
}): string[] => { | ||
return Array.from(workspaces) | ||
.map(({ cwd }) => getAllWorkspacesNonRemovablesHelper({ cwd, rootDir })) | ||
.flat(); | ||
}; |
29 changes: 29 additions & 0 deletions
29
packages/plugins/plugin-build/src/commands/bundle/ignore/getExcludedFiles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Filename } from "@yarnpkg/fslib"; | ||
import { getIgnoreFile } from "./getIgnoreFile"; | ||
import { getIgnoreFilePath } from "./getIgnoreFilePath"; | ||
import ignore from "ignore"; | ||
import { getAllFiles } from "./getAllFiles"; | ||
interface GetExcludedFilesProps { | ||
ignoreFile: Filename; | ||
exclude: string[]; | ||
cwd: string; | ||
} | ||
|
||
export const getExcludedFiles = async ({ | ||
exclude, | ||
ignoreFile: _ignoreFile, | ||
cwd, | ||
}: GetExcludedFilesProps): Promise<Filename[]> => { | ||
const ignoreFile = getIgnoreFilePath({ ignoreFile: _ignoreFile, cwd }); | ||
|
||
const ignores = ignore().add([ | ||
...exclude, | ||
...(await getIgnoreFile(ignoreFile)), | ||
]); | ||
const allFiles = await getAllFiles({ cwd }); | ||
const removeFiles = allFiles | ||
.filter((fileName) => ignores.ignores(fileName)) | ||
.map((fileName) => `${cwd}/${fileName}`); | ||
|
||
return removeFiles as Filename[]; | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/plugins/plugin-build/src/commands/bundle/ignore/getIgnoreFile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Filename } from "@yarnpkg/fslib"; | ||
import { readFileSync } from "fs"; | ||
|
||
export const getIgnoreFile = async (fileName: Filename): Promise<string[]> => { | ||
try { | ||
const data = readFileSync(fileName, "utf-8"); | ||
|
||
return data.split("\n"); | ||
} catch (_e) { | ||
// File does not exist. | ||
return []; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
packages/plugins/plugin-build/src/commands/bundle/ignore/getIgnoreFilePath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Filename } from "@yarnpkg/fslib"; | ||
import { join } from "path"; | ||
|
||
interface GetIgnoreFileProps { | ||
ignoreFile: Filename; | ||
cwd: string; | ||
} | ||
|
||
export const getIgnoreFilePath = ({ | ||
ignoreFile, | ||
cwd, | ||
}: GetIgnoreFileProps): Filename => { | ||
return join(cwd, ignoreFile) as Filename; | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/plugins/plugin-build/src/commands/bundle/ignore/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./const"; | ||
export * from "./getExcludedFiles"; | ||
export * from "./getAllWorkspacesNonRemovables"; |
Oops, something went wrong.