Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JeroenVinke/be137531a7c4a6c0c7cd0c077509a797 to your computer and use it in GitHub Desktop.
Save JeroenVinke/be137531a7c4a6c0c7cd0c077509a797 to your computer and use it in GitHub Desktop.
require('aurelia-polyfills');
const gulp = require('gulp');
const Container = require('aurelia-dependency-injection').Container;
const CLIOptions = require('../../lib/cli-options').CLIOptions;
const definition = require('../../lib/commands/new/new-application.json');
const WorkflowEngine = require('../../lib/workflow/workflow-engine').WorkflowEngine;
const ProjectCreate = require('../../lib/workflow/activities/project-create');
const ProjectInstall = require('../../lib/workflow/activities/project-install');
const UI = require('../../lib/ui').UI;
const ConsoleUI = require('../../lib/ui').ConsoleUI;
const fs = require('fs');
const LogManager = require('aurelia-logging');
const Logger = require('../../lib/logger').Logger;
gulp.task('generate-projects', function(done) {
LogManager.addAppender(new Logger(new ConsoleUI()));
const ui = new ConsoleUI();
ui.question('Where would you like to create the projects?')
.then(dir => {
return ui.question('Would you like to install NPM dependencies too?', [{
displayName: 'Yes',
value: 'yes',
description: 'Install NPM dependencies after the projects have been created',
}, {
displayName: 'No',
value: 'no',
description: 'Just create the projects'
}])
.then(installAnswer => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
process.chdir(dir);
createProjectsInSeries(require('./skeletons.json'), installAnswer.value === 'yes')
.then(() => {
console.log('Created all projects');
done();
})
.catch(e => {
console.log(e);
done();
});
});
});
});
function createProjectsInSeries(instructions, install) {
let i = 0;
function createP(instruction) {
return createProject(instruction, install)
.then(() => {
i++;
if (i < instructions.length) {
return createP(instructions[i]);
}
});
}
return createP(instructions[i]);
}
function createProject(answers, install) {
let container = new Container();
let engine = new WorkflowEngine(
definition,
container
);
container.registerInstance(UI, new ConsoleUI());
container.unregister('project-install');
container.registerInstance('project-install', new AutoProjectInstall(install));
container.unregister('project-create');
container.registerSingleton('project-create', AutoProjectCreate);
container.unregister('input-text');
container.registerInstance('input-text', new AutoInputText(answers));
container.unregister('input-select');
container.registerInstance('input-select', new AutoInputSelect(answers));
container.unregister('input-multiselect');
container.registerInstance('input-multiselect', new AutoInputMultiSelect(answers));
return engine.start()
.then(() => console.log(`Creating project`))
.catch(e => {
console.log('error while creating project');
console.log(e);
throw e;
});
}
class AutoInputSelect {
constructor(answers) {
this._answers = answers;
}
execute(context) {
let answer = this._answers[this.id];
if (!answer) {
answer = this.options[0].value;
}
context.state[this.stateProperty] = answer;
context.next(this.nextActivity);
}
}
class AutoInputMultiSelect {
constructor(answers) {
this._answers = answers;
}
execute(context) {
let answer = this._answers[this.id];
if (!answer) {
answer = [this.options[0].value];
}
context.state[this.stateProperty] = answer;
context.next(this.nextActivity);
}
}
class AutoInputText {
constructor(answers) {
this._answers = answers;
}
execute(context) {
let answer = this._answers[this.id];
if (!answer) {
throw new Error('AutoInputText has no answer for activityId ' + this.id);
}
context.state[this.stateProperty] = answer;
context.next(this.nextActivity);
}
}
class AutoProjectCreate extends ProjectCreate {
projectConfirmation() {
return Promise.resolve({ value: 'yes' });
}
}
class AutoProjectInstall extends ProjectInstall {
constructor(installProject) {
let ui = new ConsoleUI();
ui.question = () => {
if (installProject) {
return Promise.resolve({
displayName: 'Yes',
description: 'Installs all server, client and tooling dependencies needed to build the project.',
value: 'yes'
});
}
return Promise.resolve({
displayName: 'No',
description: 'Completes the new project wizard without installing dependencies.',
value: 'no'
});
};
let options = CLIOptions;
super(ui, options);
}
}
process.on('unhandledRejection', (reason) => {
console.log('Uncaught promise rejection:');
console.log(reason);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment