Skip to content
Permalink
Browse files

feat(core): add a force flag to init to allow it to overwrite an exis…

…ting directory (#1020)
  • Loading branch information
malept committed Jul 17, 2019
1 parent 745e768 commit dcdc2a1cd70f16620c5f6cc5473baf0c0ea4aba7
@@ -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;

@@ -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`);
}
}
});
};
@@ -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
*/
@@ -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);
@@ -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');
});

0 comments on commit dcdc2a1

Please sign in to comment.
You can’t perform that action at this time.