Skip to content

Commit

Permalink
feat: introduce clean/no-clean flag to disable automatic cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ZauberNerd committed Dec 13, 2020
1 parent d355032 commit 4be6736
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/__tests__/_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function partialConfig({
rootManifest = {},
}: Partial<Config> = {}): Config {
return {
clean: true,
repositories,
targetDirectory,
yarnArguments,
Expand Down Expand Up @@ -36,6 +37,7 @@ export function partialWorkspacesConfig({
rootManifest = {},
}: Partial<WorkspacesConfig> = {}): WorkspacesConfig {
return {
clean: true,
repositories,
targetDirectory,
yarnArguments,
Expand Down
44 changes: 32 additions & 12 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('normalize config', () => {
it('should throw an error if no arguments are passed', () => {
// $ canarist
expect(() => {
normalizeConfig({ _: [], help: false }, null);
normalizeConfig({ _: [], help: false, clean: true }, null);
}).toThrow(/No repositories are passed through arguments/);
});

Expand All @@ -26,6 +26,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: 'a-repo',
},
null
Expand All @@ -49,6 +50,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: 'enc:G6VSEW8lxBM3OYn7E3k4yGH61ExqKxx/rsUtKS/h8GU=',
},
null
Expand All @@ -72,6 +74,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: ['a-repo', 'b-repo'],
},
null
Expand Down Expand Up @@ -99,6 +102,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: [
{
_: ['a-repo'],
Expand All @@ -118,6 +122,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: [
{
_: ['a-repo'],
Expand All @@ -137,6 +142,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: [
{
_: ['a-repo'],
Expand All @@ -156,6 +162,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: [
{
_: ['a-repo'],
Expand All @@ -178,6 +185,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: ['https://github.com/xing/canarist.git'],
},
null
Expand All @@ -192,6 +200,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: ['.'],
},
null
Expand All @@ -206,6 +215,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: ['.'],
},
null
Expand All @@ -217,7 +227,7 @@ describe('normalize config', () => {
it('should accept target directory', () => {
// $ canarist -r . /some/dir
const config = normalizeConfig(
{ _: ['/some/dir'], help: false, repository: '.' },
{ _: ['/some/dir'], help: false, clean: true, repository: '.' },
null
);

Expand All @@ -227,7 +237,7 @@ describe('normalize config', () => {
it('should default target directory to /tmp/canarist', () => {
// $ canarist -r .
const config = normalizeConfig(
{ _: [], help: false, repository: '.' },
{ _: [], help: false, clean: true, repository: '.' },
null
);

Expand All @@ -240,6 +250,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: '.',
'root-manifest': '{"devDependencies":{"jest":"^25"}}',
},
Expand All @@ -255,6 +266,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: '.',
},
null
Expand All @@ -269,6 +281,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: '.',
'root-manifest': '{"devDepen',
},
Expand All @@ -284,6 +297,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: '.',
'yarn-arguments': '--production=false',
},
Expand All @@ -299,6 +313,7 @@ describe('normalize config', () => {
{
_: [],
help: false,
clean: true,
repository: '.',
},
null
Expand All @@ -313,7 +328,7 @@ describe('normalize config', () => {
// $ canarist
expect(() => {
normalizeConfig(
{ _: [], help: false },
{ _: [], help: false, clean: true },
{
filepath: '',
config: {
Expand All @@ -327,7 +342,7 @@ describe('normalize config', () => {
it('should return config if no projects are configured', () => {
// $ canarist
const config = normalizeConfig(
{ _: [], help: false },
{ _: [], help: false, clean: true },
{
filepath: '',
config: {
Expand Down Expand Up @@ -358,13 +373,14 @@ describe('normalize config', () => {
rootManifest: { devDependencies: { jest: '^25' } },
targetDirectory: '/some/directory',
yarnArguments: '--production=false',
clean: true,
});
});

it('should leave branch blank when cloning from a local path', () => {
// $ canarist
const config = normalizeConfig(
{ _: [], help: false },
{ _: [], help: false, clean: true },
{
filepath: '',
config: {
Expand All @@ -389,14 +405,15 @@ describe('normalize config', () => {
rootManifest: {},
targetDirectory: '/tmp/canarist-XXXXXX',
yarnArguments: '',
clean: true,
});
});

it('should throw an error if no project is selected', () => {
// $ canarist
expect(() => {
normalizeConfig(
{ _: [], help: false },
{ _: [], help: false, clean: true },
{
filepath: '',
config: {
Expand All @@ -411,7 +428,7 @@ describe('normalize config', () => {
// $ canarist -p a-project
expect(() => {
normalizeConfig(
{ _: [], help: false, project: 'a-project' },
{ _: [], help: false, clean: true, project: 'a-project' },
{
filepath: '',
config: {
Expand All @@ -431,7 +448,7 @@ describe('normalize config', () => {
// $ canarist -p a-project
expect(() => {
normalizeConfig(
{ _: [], help: false, project: 'a-project' },
{ _: [], help: false, clean: true, project: 'a-project' },
{
filepath: '',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -444,7 +461,7 @@ describe('normalize config', () => {
it('should return config of selected project with defaults', () => {
// $ canarist -p a-project
const config = normalizeConfig(
{ _: [], help: false, project: 'a-project' },
{ _: [], help: false, clean: true, project: 'a-project' },
{
filepath: '',
config: {
Expand All @@ -470,13 +487,14 @@ describe('normalize config', () => {
rootManifest: {},
targetDirectory: '/tmp/canarist-XXXXXX',
yarnArguments: '',
clean: true,
});
});

it('should return config of selected project', () => {
// $ canarist -p a-project
const config = normalizeConfig(
{ _: [], help: false, project: 'a-project' },
{ _: [], help: false, clean: true, project: 'a-project' },
{
filepath: '',
config: {
Expand Down Expand Up @@ -512,14 +530,15 @@ describe('normalize config', () => {
rootManifest: { devDependencies: { jest: '^25' } },
targetDirectory: '/some/directory',
yarnArguments: '--production=false',
clean: true,
});
});
});

it('should give precedence to CLI config', () => {
// $ canarist -r b-repo
const config = normalizeConfig(
{ _: [], help: false, repository: 'b-repo' },
{ _: [], help: false, clean: true, repository: 'b-repo' },
{
filepath: '',
config: {
Expand Down Expand Up @@ -555,6 +574,7 @@ describe('normalize config', () => {
rootManifest: {},
targetDirectory: '/tmp/canarist-XXXXXX',
yarnArguments: '',
clean: true,
});
});
});
11 changes: 9 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const minimistConfig: Opts = {
'yarn-arguments': ['y'],
project: ['p'],
},
boolean: ['help'],
boolean: ['help', 'clean'],
string: [
'repository',
'branch',
Expand All @@ -41,6 +41,9 @@ const minimistConfig: Opts = {
'yarn-arguments',
'project',
],
default: {
clean: true,
},
};

function printUsage(): void {
Expand All @@ -63,6 +66,8 @@ Options:
Additional arguments that should be passed to the "yarn install" command.
--project, -p
The name of a project to execute in a multi-project configuration.
--clean
Remove the target directory after canarist exits. Default: true
Examples:
$ canarist -r git@github.com:xing/canarist.git -r git@github.com:some/other.git
Expand Down Expand Up @@ -166,7 +171,9 @@ try {

console.log('[canarist] finished successfully!');
} finally {
rimrafSync(config.targetDirectory);
if (config.clean) {
rimrafSync(config.targetDirectory);
}
}
} catch (err) {
process.exitCode = 1;
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface RepositoryConfig {
}

export interface Config {
clean: boolean;
targetDirectory: string;
rootManifest: Partial<PackageJSON>;
yarnArguments: string;
Expand Down Expand Up @@ -46,6 +47,7 @@ interface RepositoryArguments extends minimist.ParsedArgs {

export interface Arguments extends minimist.ParsedArgs {
help: boolean;
clean: boolean;
repository?: string | (string | RepositoryArguments)[];
'root-manifest'?: string;
'yarn-arguments'?: string;
Expand Down Expand Up @@ -219,6 +221,7 @@ export function normalizeConfig(
}

return {
clean: argv.clean,
targetDirectory,
rootManifest,
yarnArguments,
Expand Down

0 comments on commit 4be6736

Please sign in to comment.