Skip to content

Commit

Permalink
Ignore file #97 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
lodmfjord authored Oct 21, 2021
1 parent e23ccc5 commit fef3ff3
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 40 deletions.
57 changes: 57 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
"is-ci": "^3.0.0"
},
"packageManager": "[email protected]"
}
}
3 changes: 3 additions & 0 deletions packages/plugins/plugin-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"await-semaphore": "0.1.3",
"clipanion": "^3.x",
"date-fns": "^2.12.0",
"glob": "^7.2.0",
"glob-promise": "^4.2.1",
"ignore": "^5.1.8",
"is-ci": "^2.0.0",
"p-limit": "^2.3.0",
"p-queue": "^6.3.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/plugins/plugin-build/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Command, Option, Usage } from "clipanion";
import path from "path";

import { EventEmitter } from "events";
import { GetPluginConfiguration, YarnBuildConfiguration } from "../../config";
import { GetPluginConfiguration } from "../../config";
import RunSupervisor, { RunSupervisorReporterEvents } from "../../supervisor";

import { addTargets } from "../../supervisor/workspace";
Expand Down Expand Up @@ -77,8 +77,10 @@ export default class Build extends BaseCommand {
this.context.plugins
);

const pluginConfiguration: YarnBuildConfiguration =
await GetPluginConfiguration(configuration);
const pluginConfiguration = await GetPluginConfiguration(configuration);

this.shouldBailInstantly =
this.shouldBailInstantly ?? pluginConfiguration.bail;

this.shouldBailInstantly =
this.shouldBailInstantly ?? pluginConfiguration.bail;
Expand Down
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;
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 [];
}
};
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();
};
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[];
};
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 [];
}
};
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;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./const";
export * from "./getExcludedFiles";
export * from "./getAllWorkspacesNonRemovables";
Loading

0 comments on commit fef3ff3

Please sign in to comment.