Skip to content

Commit

Permalink
fix(core): throw an error if generating a new workspace into a non-em…
Browse files Browse the repository at this point in the history
…pty directory (#5296)
  • Loading branch information
FrozenPandaz committed Apr 15, 2021
1 parent 656d808 commit 949cf31
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/workspace/src/generators/new/new.spec.ts
Expand Up @@ -106,4 +106,19 @@ describe('new', () => {
expect(readJson(tree, 'package.json')).toEqual(packageJson);
expect(readJson(tree, '.eslintrc.json')).toEqual(eslintConfig);
});

it('should throw an error when the directory is not empty', async () => {
tree.write('my-workspace/file.txt', '');

try {
await newGenerator(tree, {
...defaultOptions,
name: 'my-workspace',
directory: 'my-workspace',
npmScope: 'npmScope',
appName: 'app',
});
fail('Generating into a non-empty directory should error.');
} catch (e) {}
});
});
10 changes: 10 additions & 0 deletions packages/workspace/src/generators/new/new.ts
Expand Up @@ -171,6 +171,16 @@ export async function newGenerator(host: Tree, options: Schema) {

options = normalizeOptions(options);

if (
host.exists(options.name) &&
!host.isFile(options.name) &&
host.children(options.name).length > 0
) {
throw new Error(
`${join(host.root, options.name)} is not an empty directory.`
);
}

const layout: 'packages' | 'apps-and-libs' =
options.preset === 'oss' ? 'packages' : 'apps-and-libs';
const workspaceOpts = {
Expand Down

0 comments on commit 949cf31

Please sign in to comment.