Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Checking if files exists before writing in generate. Allowing --force/-f to bypass. #500

Merged
merged 4 commits into from
Dec 4, 2018
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
73 changes: 40 additions & 33 deletions cli/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@

const fs = require('fs-extra');
const path = require('path');
const logger = require('@blackbaud/skyux-logger');

function resolveFilePath(pathParts, fileName) {
fs.ensureDirSync(path.resolve('src', 'app', ...pathParts));
function safeFileWrite(pathParts, fileName, fileContent, force) {
const resolvedFileName = path.resolve('src', 'app', ...pathParts, fileName);
const resolvedFileExists = fs.existsSync(resolvedFileName);

if (resolvedFileExists && !force) {
logger.warn(`${resolvedFileName} already exists. Use --force to overwrite.`);
return;
}

if (resolvedFileExists) {
logger.warn(`${resolvedFileName} already exists. Forcefully overwriting.`);
}

return path.resolve('src', 'app', ...pathParts, fileName);
fs.ensureDirSync(path.resolve('src', 'app', ...pathParts));
fs.writeFileSync(resolvedFileName, fileContent);
logger.info(`Successfully created ${resolvedFileName}.`);
}

function properCase(name) {
Expand Down Expand Up @@ -45,9 +58,8 @@ function snakeCase(name) {
return nameSnake;
}

function generateComponentTs(pathParts, fileName, name, nameSnakeCase) {
fs.writeFileSync(
resolveFilePath(pathParts, fileName + '.ts'),
function generateComponentTs(pathParts, fileName, name, nameSnakeCase, force) {
const fileContent =
`import {
Component
} from '@angular/core';
Expand All @@ -60,15 +72,14 @@ function generateComponentTs(pathParts, fileName, name, nameSnakeCase) {
export class ${name} {

}
`
);
}
`;

function generateComponentSpec(pathParts, fileName, name, nameSnakeCase) {
let nameWithSpaces = properCase(nameSnakeCase.replace(/\-/g, ' '));
safeFileWrite(pathParts, `${fileName}.ts`, fileContent, force);
}

fs.writeFileSync(
resolveFilePath(pathParts, fileName + '.spec.ts'),
function generateComponentSpec(pathParts, fileName, name, nameSnakeCase, force) {
const nameWithSpaces = properCase(nameSnakeCase.replace(/\-/g, ' '));
const fileContent =
`import {
TestBed
} from '@angular/core/testing';
Expand Down Expand Up @@ -105,29 +116,24 @@ describe('${nameWithSpaces} component', () => {
});

});
`
);
`;

safeFileWrite(pathParts, `${fileName}.spec.ts`, fileContent, force);
}

function generateComponentHtml(pathParts, fileName) {
fs.writeFileSync(
resolveFilePath(pathParts, fileName + '.html'),
''
);
function generateComponentHtml(pathParts, fileName, force) {
safeFileWrite(pathParts, `${fileName}.html`, '', force);
}

function generateComponentScss(pathParts, fileName) {
fs.writeFileSync(
resolveFilePath(pathParts, fileName + '.scss'),
''
);
function generateComponentScss(pathParts, fileName, force) {
safeFileWrite(pathParts, `${fileName}.scss`, '', force);
}

function getPathParts(name) {
return name.replace(/\\/g, '/').split('/');
}

function generateComponent(name) {
function generateComponent(name, force) {
const pathParts = getPathParts(name);

const classNameWithoutComponent = properCase(pathParts.pop());
Expand All @@ -138,21 +144,22 @@ function generateComponent(name) {

const fileName = `${nameSnakeCase}.component`;

generateComponentTs(pathParts, fileName, className, nameSnakeCase);
generateComponentSpec(pathParts, fileName, className, nameSnakeCase);
generateComponentHtml(pathParts, fileName);
generateComponentScss(pathParts, fileName);
generateComponentTs(pathParts, fileName, className, nameSnakeCase, force);
generateComponentSpec(pathParts, fileName, className, nameSnakeCase, force);
generateComponentHtml(pathParts, fileName, force);
generateComponentScss(pathParts, fileName, force);
}

function generate(argv) {
try {
let type = argv._[1];
let name = argv._[2];
const type = argv._[1];
const name = argv._[2];
const force = argv.force;

switch (type) {
case 'component':
case 'c':
generateComponent(name);
generateComponent(name, force);
break;
}
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function getConfig(command) {
module.exports = {
runCommand: (command, argv) => {
const shorthand = {
f: 'force',
l: 'launch',
b: 'browser',
s: 'serve'
Expand Down Expand Up @@ -52,6 +53,7 @@ module.exports = {
require('./cli/version')();
break;
case 'generate':
case 'g':
require('./cli/generate')(argv);
break;
default:
Expand Down
54 changes: 53 additions & 1 deletion test/cli-generate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ describe('cli generate', () => {

fsMock = {
ensureDirSync: jasmine.createSpy('ensureDirSync'),
writeFileSync: jasmine.createSpy('writeFileSync')
writeFileSync: jasmine.createSpy('writeFileSync'),
existsSync: jasmine.createSpy('existsSync').and.returnValue(false)
};

mock('fs-extra', fsMock);
mock('@blackbaud/skyux-logger', jasmine.createSpyObj('logger', ['info', 'warn']));

spyOn(process, 'exit').and.returnValue();

Expand Down Expand Up @@ -155,4 +157,54 @@ export class ${expectedClassName}`
1
);
});

function generateSafe(force) {
const name = 'my-component';
const pathMock = jasmine.createSpyObj('path', ['resolve']);
const fsMock = jasmine.createSpyObj('fs', [
'ensureDirSync',
'existsSync',
'writeFileSync'
]);
const loggerMock = jasmine.createSpyObj('logger', ['info', 'warn']);

pathMock.resolve.and.returnValue('resolved-' + name);
fsMock.existsSync.and.returnValue(true);

mock('path', pathMock);
mock('fs-extra', fsMock);
mock('@blackbaud/skyux-logger', loggerMock);

const generate = mock.reRequire('../cli/generate');
generate({
_: [
'generate',
'component',
name
],
force: force
});

if (force) {
expect(fsMock.writeFileSync).toHaveBeenCalled();
expect(loggerMock.warn.calls.argsFor(0)).toEqual([
`resolved-${name} already exists. Forcefully overwriting.`
]);
} else {
expect(fsMock.writeFileSync).not.toHaveBeenCalled();
expect(loggerMock.warn).toHaveBeenCalledWith(
`resolved-${name} already exists. Use --force to overwrite.`
);
}

mock.stopAll();
}

it('should log a warning and exit if a file already exists', () => {
generateSafe(false);
});

it('should log a warning and continue writing if force is supplied', () => {
generateSafe(true);
});
});
8 changes: 7 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('@blackbaud/skyux-builder', () => {
'generate': {
cmd: 'generate',
lib: 'generate'
},
'g': {
cmd: 'generate',
lib: 'generate'
}
};

Expand Down Expand Up @@ -91,11 +95,13 @@ describe('@blackbaud/skyux-builder', () => {
it('should process shorthand tags', (done) => {
const argv = {
l: 'showForLaunch',
b: 'showForBrowser'
b: 'showForBrowser',
f: 'showForForce'
};
mock('../cli/test', (c, a) => {
expect(a.launch).toEqual(argv.l);
expect(a.browser).toEqual(argv.b);
expect(a.force).toEqual(argv.f);
done();
});
const lib = require('../index');
Expand Down