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
  • Loading branch information
malept committed Jul 13, 2019
1 parent 1d19b32 commit a6938b0
Show file tree
Hide file tree
Showing 4 changed files with 26 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: false,
};
if (program.template) initOpts.template = program.template;

Expand Down
14 changes: 10 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 @@ -2,19 +2,25 @@ import { asyncOra } from '@electron-forge/async-ora';
import debug from 'debug';
import fs from 'fs-extra';
import logSymbols from 'log-symbols';
import { warn } from '../../util/messages';

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

export default async (dir: string) => {
export default async (dir: string, { force = false, interactive = 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) {
warn(interactive, `The specified path "${dir}" is not empty. "force" was set to true, so files will be overwritten`.yellow);
} 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, interactive });
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 @@ -121,6 +121,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 a6938b0

Please sign in to comment.