Skip to content

Commit

Permalink
feat(core): add a force flag to init to allow it to overwrite an exis…
Browse files Browse the repository at this point in the history
…ting directory (#1020)
  • Loading branch information
malept authored Jul 17, 2019
1 parent 745e768 commit dcdc2a1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/api/cli/src/electron-forge-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import workingDir from './util/working-dir';
.arguments('[name]')
.option('-t, --template [name]', 'Name of the Forge template to use')
.option('-c, --copy-ci-files', 'Whether to copy the templated CI files (defaults to false)', false)
.option('-f, --force', 'Whether to overwrite an existing directory (defaults to false)', false)
.action((name) => { dir = workingDir(dir, name, false); })
.parse(process.argv);

const initOpts: InitOptions = {
dir,
interactive: true,
copyCIFiles: !!program.copyCiFiles,
force: !!program.force,
};
if (program.template) initOpts.template = program.template;

Expand Down
13 changes: 9 additions & 4 deletions packages/api/core/src/api/init-scripts/init-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import logSymbols from 'log-symbols';

const d = debug('electron-forge:init:directory');

export default async (dir: string) => {
export default async (dir: string, force = false) => {
await asyncOra('Initializing Project Directory', async (initSpinner) => {
d('creating directory:', dir);
await fs.mkdirs(dir);

const files = await fs.readdir(dir);
if (files.length !== 0) {
d('found', files.length, 'files in the directory. warning the user');
initSpinner.stop(logSymbols.warning);
throw new Error(`The specified path: "${dir}" is not empty. Please ensure it is empty before initializing a new project`);
d(`found ${files.length} files in the directory. warning the user`);

if (force) {
initSpinner.warn(`The specified path "${dir}" is not empty. "force" was set to true, so proceeding to initialize. Files may be overwritten`);
} else {
initSpinner.stop(logSymbols.warning);
throw new Error(`The specified path: "${dir}" is not empty. Please ensure it is empty before initializing a new project`);
}
}
});
};
7 changes: 6 additions & 1 deletion packages/api/core/src/api/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface InitOptions {
* Whether to copy Travis and AppVeyor CI files
*/
copyCIFiles?: boolean;
/**
* Whether to overwrite an existing directory
*/
force?: boolean;
/**
* The custom template to use. If left empty, the default template is used
*/
Expand All @@ -32,13 +36,14 @@ export default async ({
dir = process.cwd(),
interactive = false,
copyCIFiles = false,
force = false,
template,
}: InitOptions) => {
asyncOra.interactive = interactive;

d(`Initializing in: ${dir}`);

await initDirectory(dir);
await initDirectory(dir, force);
await initGit(dir);
await initStarter(dir, { copyCIFiles });
await initNPM(dir);
Expand Down
8 changes: 8 additions & 0 deletions packages/api/core/test/slow/api_spec_slow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ describe(`electron-forge API (with installer=${nodeInstaller})`, () => {
})).to.eventually.be.rejected;
});

it('should initialize an already initialized directory when forced to', async () => {
await forge.init({
dir,
force: true,
template: 'webpack',
});
});

it('should add a devDependency on @electron-forge/plugin-webpack', async () => {
expect(Object.keys(require(path.resolve(dir, 'package.json')).devDependencies)).to.contain('@electron-forge/plugin-webpack');
});
Expand Down

0 comments on commit dcdc2a1

Please sign in to comment.