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

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzofish committed Aug 7, 2017
2 parents 14112f6 + 59de724 commit bf19432
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 54 deletions.
6 changes: 3 additions & 3 deletions commands/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const getRemainingQuestions = (selectorName, options) => {
};

const getSelector = (selector) => {
if (utilities.checkIsDashFormat(selector)) {
if (caseConvert.checkIsDashFormat(selector)) {
return { answers: getKnownSelector(selector) };
} else {
return {
Expand All @@ -104,12 +104,12 @@ const getSelector = (selector) => {

const getKnownSelector = (selector) => [
{ answer: selector, name: 'selector' },
{ answer: utilities.dashToCap(selector) + 'Component', name: 'componentName' }
{ answer: caseConvert.dashToCap(selector) + 'Component', name: 'componentName' }
];

const getSelectorQuestions = () => [
{ name: 'selector', question: 'What is the component selector (in dash-case)?', transform: (value) => caseConvert.checkIsDashFormat(value) ? value : null },
{ name: 'componentName', transform: (value) => utilities.dashToCap(value) + 'Component', useAnswer: 'selector' }
{ name: 'componentName', transform: (value) => caseConvert.dashToCap(value) + 'Component', useAnswer: 'selector' }
];

const getStyle = (options) => {
Expand Down
3 changes: 2 additions & 1 deletion commands/initial/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ module.exports = (rootDir, ...args) => {
{ name: 'webpack/webpack.test.js', overwrite: true },
{ name: 'webpack/webpack.utils.js', overwrite: true }
];
const librarianVersion = files.librarianVersions.get();
const gitAnswer = answers.find((answer) => answer.name === 'git');
const templates = files.getTemplates(rootDir, __dirname, templateList);
const results = erector.construct(answers, templates);
const results = erector.construct(answers.concat({ answer: librarianVersion, name: 'librarianVersion' }), templates);

process.chdir(rootDir);
if (gitAnswer.answer) {
Expand Down
2 changes: 1 addition & 1 deletion commands/initial/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@angular/compiler-cli": "^4.0.0",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"angular-librarian": "1.0.0-beta.13",
"angular-librarian": "{{ librarianVersion }}",
"angular2-template-loader": "0.6.0",
"awesome-typescript-loader": "^3.0.0",
"codelyzer": "~2.0.0-beta.1",
Expand Down
51 changes: 35 additions & 16 deletions commands/upgrade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const childProcess = require('child_process');

const logging = require('../../tools/logging');
const { colorize, files, execute, inputs } = require('../../tools/utilities');
const { librarianVersions } = files;

let logger;

Expand All @@ -15,25 +16,28 @@ module.exports = (rootDir) => {
/* istanbul ignore next */
const npmCommand = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';

return getLibrarianVersions(npmCommand)
.then((versions) => installLibrarian(npmCommand, versions))
return upgradeLibrarian(npmCommand)
.then(upgradeFiles);
};

const upgradeFiles = () => erector.inquire([
{
allowBlank: true,
name: 'proceed',
question: 'The following will overwrite some of the files in your project. Would you like to continue (y/N)?',
transform: inputs.createYesNoValue('n', [])
}
]).then((answers) => {
if (answers[0].answer) {
updateFiles();
const upgradeLibrarian = (npmCommand) => {
const version = librarianVersions.get();

if (!librarianVersions.checkIsBranch(version)) {
return getLibrarianVersions(npmCommand)
.then((versions) => installLibrarian(npmCommand, versions));
} else {
logger.info(colorize.colorize(' Upgrade cancelled.', 'yellow'));
return Promise.resolve()
.then(() => upgradeBranchLibrarian(npmCommand, version));
}
});
};

const upgradeBranchLibrarian = (npm, version) => {
logger.info(colorize.colorize('Upgrading angular-librarian from:', 'green'));
logger.info(colorize.colorize(' ' + version, 'magenta'));

execute.execute(npm, ['up', 'angular-librarian']);
}

const getLibrarianVersions = (npm) => new Promise((resolve, reject) => {
logger.info(colorize.colorize('Identifying the *newest* angular-librarian version', 'cyan'));
Expand Down Expand Up @@ -73,7 +77,7 @@ const installLibrarian = (npm, { available, installed}) => {
const update = require('semver').gt(available, installed);

logger.info(
colorize.colorize('\tUpdate of angular-librarian is', 'yellow'),
colorize.colorize('\tUpgrade of angular-librarian is', 'yellow'),
update ? '' : colorize.colorize('NOT', 'red'),
colorize.colorize('required.', 'yellow')
);
Expand All @@ -84,9 +88,24 @@ const installLibrarian = (npm, { available, installed}) => {
}
}

const upgradeFiles = () => erector.inquire([
{
allowBlank: true,
name: 'proceed',
question: 'The following will overwrite some of the files in your project. Would you like to continue (y/N)?',
transform: inputs.createYesNoValue('n', [])
}
]).then((answers) => {
if (answers[0].answer) {
updateFiles();
} else {
logger.info(colorize.colorize(' Upgrade cancelled.', 'yellow'));
}
});

const updateFiles = () => {
logger.info(colorize.colorize(' Updating managed files to latest versions', 'cyan'));
const answers = getErectorAnswers();
const answers = getErectorAnswers().concat({ answer: librarianVersions.get(), name: 'librarianVersion' });
const srcDir = files.resolver.create('src');
const fileList = [
{ destination: files.resolver.root('.gitignore'), name: '__gitignore', update: updateFlatFile },
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"devDependencies": {
"nodemon": "^1.11.0",
"sinon": "^2.3.8",
"tap": "^10.7.0"
"sinon": "^2.4.1",
"tap": "^10.7.1"
}
}
11 changes: 10 additions & 1 deletion test/initial.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const sandbox = sinon.sandbox.create();
tap.test('command: initial', (suite) => {
let chdir;
let execSync;
let filesBranch;
let filesInclude;
let filesVersions;
let getTemplates;
let mockErector;
let mockLog;
Expand All @@ -34,7 +36,9 @@ tap.test('command: initial', (suite) => {

chdir = sandbox.stub(process, 'chdir');
execSync = sandbox.stub(childProcess, 'execSync');
filesBranch = sandbox.stub(files.librarianVersions, 'checkIsBranch');
filesInclude = sandbox.stub(files, 'include');
filesVersions = sandbox.stub(files.librarianVersions, 'get');
getTemplates = sandbox.stub(files, 'getTemplates');
mockLog = sandbox.spy();
mockLogger = sandbox.stub(logger, 'create');
Expand All @@ -46,6 +50,7 @@ tap.test('command: initial', (suite) => {
return { name: 'fake-library' };
}
});
filesVersions.returns('ice-cream');
mockErector = {
construct: sandbox.stub(erector, 'construct'),
inquire: sandbox.stub(erector, 'inquire')
Expand Down Expand Up @@ -273,6 +278,10 @@ tap.test('command: initial', (suite) => {
{ answer: '1.0.0', name: 'version' }
];
const dirname = process.cwd() + path.sep + ['commands', 'initial'].join(path.sep);
const finalAnswers = answers.concat({
answer: 'ice-cream',
name: 'librarianVersion'
});
const templates = [
{ destination: '/root/path', template: 'some/template/path' },
{ desetination: '/root/blank-file', template: undefined }
Expand All @@ -289,7 +298,7 @@ tap.test('command: initial', (suite) => {
test.ok(chdir.calledWith('./'));
test.ok(chdir.calledWith(dirname));

test.ok(mockErector.construct.calledWith(answers, templates));
test.ok(mockErector.construct.calledWith(finalAnswers, templates));
test.end();
});
});
Expand Down
32 changes: 32 additions & 0 deletions test/npm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const childProcess = require('child_process');
const sinon = require('sinon');
const tap = require('tap');

const colorize = require('../tools/utilities/colorize');
const logging = require('../tools/logging');
const npm = require('../commands/npm');

const cmd = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
Expand Down Expand Up @@ -76,5 +78,35 @@ tap.test('command: npm', (suite) => {
test.end();
});

suite.test('should return a promise', (test) => {
test.plan(1);

npm('./', 'b').then(() => {
test.ok(true);
test.end();
});
});

suite.test('should reject if an error happens', (test) => {
const color = sandbox.stub(colorize, 'colorize');
const log = sandbox.spy();
const logger = sandbox.stub(logging, 'create');

color.callsFake((text, color) => `[${ color }]${ text }[/${ color }]`);
logger.returns({
error: log
});
spawn.callsFake(() => {
throw new Error(`I'm afraid I've got some bad news!`);
});

test.plan(1);

npm('./').catch((error) => {
test.equal(error, `I'm afraid I've got some bad news!`);
test.end();
});
});

suite.end();
});
4 changes: 4 additions & 0 deletions test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ exports.makeMake = (command) =>

exports.mock = (sandbox) => {
const mocks = {
checkVersion: sandbox.stub(utilities.files.librarianVersions, 'checkIsBranch'),
colorize: sandbox.stub(utilities.colorize, 'colorize'),
erector: {
construct: sandbox.stub(erector, 'construct'),
inquire: sandbox.stub(erector, 'inquire')
},
getTemplates: sandbox.stub(utilities.files, 'getTemplates'),
getVersion: sandbox.stub(utilities.files.librarianVersions, 'get'),
log: sandbox.spy(),
logger: sandbox.stub(logging, 'create'),
parseOptions: sandbox.stub(utilities.options, 'parseOptions'),
Expand All @@ -38,11 +40,13 @@ exports.mock = (sandbox) => {

erector.construct.setTestMode(true);

mocks.checkVersion.returns(false);
mocks.colorize.callsFake((text, color) =>
`[${ color }]${ text }[/${ color }]`
);
mocks.erector.inquire.rejects();
mocks.getTemplates.returns('fake-templates');
mocks.getVersion.returns('ice-cream');
mocks.logger.returns({
error: mocks.log,
info: mocks.log,
Expand Down
Loading

0 comments on commit bf19432

Please sign in to comment.