Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add "init --force" to allow overwriting an existing dir #1020

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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