From 4be673692fe10cbc19c9fe41845181e79eccd29d Mon Sep 17 00:00:00 2001 From: ZauberNerd Date: Thu, 10 Dec 2020 17:55:10 +0100 Subject: [PATCH] feat: introduce clean/no-clean flag to disable automatic cleanup --- src/__tests__/_helpers.ts | 2 ++ src/__tests__/config.test.ts | 44 ++++++++++++++++++++++++++---------- src/bin.ts | 11 +++++++-- src/config.ts | 3 +++ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/__tests__/_helpers.ts b/src/__tests__/_helpers.ts index 3c68bc6..81c679b 100644 --- a/src/__tests__/_helpers.ts +++ b/src/__tests__/_helpers.ts @@ -8,6 +8,7 @@ export function partialConfig({ rootManifest = {}, }: Partial = {}): Config { return { + clean: true, repositories, targetDirectory, yarnArguments, @@ -36,6 +37,7 @@ export function partialWorkspacesConfig({ rootManifest = {}, }: Partial = {}): WorkspacesConfig { return { + clean: true, repositories, targetDirectory, yarnArguments, diff --git a/src/__tests__/config.test.ts b/src/__tests__/config.test.ts index 005e8c7..913c240 100644 --- a/src/__tests__/config.test.ts +++ b/src/__tests__/config.test.ts @@ -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/); }); @@ -26,6 +26,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: 'a-repo', }, null @@ -49,6 +50,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: 'enc:G6VSEW8lxBM3OYn7E3k4yGH61ExqKxx/rsUtKS/h8GU=', }, null @@ -72,6 +74,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: ['a-repo', 'b-repo'], }, null @@ -99,6 +102,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: [ { _: ['a-repo'], @@ -118,6 +122,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: [ { _: ['a-repo'], @@ -137,6 +142,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: [ { _: ['a-repo'], @@ -156,6 +162,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: [ { _: ['a-repo'], @@ -178,6 +185,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: ['https://github.com/xing/canarist.git'], }, null @@ -192,6 +200,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: ['.'], }, null @@ -206,6 +215,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: ['.'], }, null @@ -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 ); @@ -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 ); @@ -240,6 +250,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: '.', 'root-manifest': '{"devDependencies":{"jest":"^25"}}', }, @@ -255,6 +266,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: '.', }, null @@ -269,6 +281,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: '.', 'root-manifest': '{"devDepen', }, @@ -284,6 +297,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: '.', 'yarn-arguments': '--production=false', }, @@ -299,6 +313,7 @@ describe('normalize config', () => { { _: [], help: false, + clean: true, repository: '.', }, null @@ -313,7 +328,7 @@ describe('normalize config', () => { // $ canarist expect(() => { normalizeConfig( - { _: [], help: false }, + { _: [], help: false, clean: true }, { filepath: '', config: { @@ -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: { @@ -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: { @@ -389,6 +405,7 @@ describe('normalize config', () => { rootManifest: {}, targetDirectory: '/tmp/canarist-XXXXXX', yarnArguments: '', + clean: true, }); }); @@ -396,7 +413,7 @@ describe('normalize config', () => { // $ canarist expect(() => { normalizeConfig( - { _: [], help: false }, + { _: [], help: false, clean: true }, { filepath: '', config: { @@ -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: { @@ -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 @@ -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: { @@ -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: { @@ -512,6 +530,7 @@ describe('normalize config', () => { rootManifest: { devDependencies: { jest: '^25' } }, targetDirectory: '/some/directory', yarnArguments: '--production=false', + clean: true, }); }); }); @@ -519,7 +538,7 @@ describe('normalize config', () => { 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: { @@ -555,6 +574,7 @@ describe('normalize config', () => { rootManifest: {}, targetDirectory: '/tmp/canarist-XXXXXX', yarnArguments: '', + clean: true, }); }); }); diff --git a/src/bin.ts b/src/bin.ts index 5f79d43..3688297 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -31,7 +31,7 @@ const minimistConfig: Opts = { 'yarn-arguments': ['y'], project: ['p'], }, - boolean: ['help'], + boolean: ['help', 'clean'], string: [ 'repository', 'branch', @@ -41,6 +41,9 @@ const minimistConfig: Opts = { 'yarn-arguments', 'project', ], + default: { + clean: true, + }, }; function printUsage(): void { @@ -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 @@ -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; diff --git a/src/config.ts b/src/config.ts index a3769b1..dce6486 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,6 +15,7 @@ export interface RepositoryConfig { } export interface Config { + clean: boolean; targetDirectory: string; rootManifest: Partial; yarnArguments: string; @@ -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; @@ -219,6 +221,7 @@ export function normalizeConfig( } return { + clean: argv.clean, targetDirectory, rootManifest, yarnArguments,