Skip to content

Instantly share code, notes, and snippets.

@vegarringdal
Last active July 3, 2016 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vegarringdal/161999c86be0383f0b06ae48daa2558e to your computer and use it in GitHub Desktop.
Save vegarringdal/161999c86be0383f0b06ae48daa2558e to your computer and use it in GitHub Desktop.
aurelia-cli generate project
"use strict";
let isLoaded = false;
exports.NPM = class {
install(packages, npmOptions, callback) {
npmOptions = npmOptions || {};
const npm = require('npm');
let originalWorkingDirectory = process.cwd();
process.chdir(npmOptions.workingDirectory || process.cwd());
return load(npm, npmOptions)
.then(() => {
return new Promise((resolve, reject) => {
// get log messages
npm.registry.log.on('log', function (message) {
//here we could maybe send messages back to GUI with progress
console.log(message)
})
npm.commands.install(packages, error => {
process.chdir(originalWorkingDirectory);
if (error) reject(error);
else resolve();
});
});
}).catch(error => {
process.chdir(originalWorkingDirectory);
throw error;
});
}
}
function load(npm, npmOptions) {
if (isLoaded) {
for(let key in npmOptions) {
npm.config.set(key, npmOptions[key]);
}
return Promise.resolve();
}
return new Promise((resolve, reject) => {
npm.load(npmOptions, error => {
if (error) reject(error);
else {
isLoaded = true;
resolve();
}
});
})
}
//simple code for also installing dependencies also in main.js
var installDep = true;
const NPM = require(path + 'npm').NPM;
function installDependencies(directory) {
"use strict";
let npm = new NPM();
let npmOptions = {
loglevel: 'error',
color: 'always',
save: true,
'save-dev': true,
workingDirectory: directory
};
return npm.install([], npmOptions)
}
project.create({}, pathToAddProject)
.then(()=> {
"use strict";
console.log("Project created");
if (!installDep) {
console.log("Done")
} else {
console.log("Installing dependencies, please wait");
installDependencies(pathToAddProject + '\\' + model.name)
.then(()=> {
"use strict";
console.log("Dependencies installed");
console.log("Done")
})
}
});
Note to myself...
Made a copy of you NPM plugin and called all that was "npm" to "jspm"
DO not use! this can mess up your project :-)
```
install() {
if (this._installing) {
alert('Already installing');
return;
}
this._installing = true;
let task = {
title: `jspm install of '${this.project.name}'`,
estimation: 'This could take minutes to complete',
logs: []
};
let workingDirectory = FS.getFolderPath(this.project.packageJSONPath);
let jspm = require("jspm");
jspm.setPackagePath(workingDirectory);
let promise = jspm.install(true, {lock:false}).then(()=>{
this._installing = false;
});
task.promise = promise;
this.taskManager.addTask(task);
}
```
//to test first install aurelia-cli locally then run this file
/***********************************************************************************
* Options to generate the project
*/
const pathToAddProject = __dirname; //lets just add same dir as this file
//model file to create the application
var model = {
"name": "testProject",
"type": "project:application",
"transpiler": {
"id": "babel",
"displayName": "Babel",
"fileExtension": ".js"
},
"markupProcessor": {
"id": "none",
"displayName": "None",
"fileExtension": ".html"
},
"cssProcessor": {
"id": "none",
"displayName": "None",
"fileExtension": ".css"
},
"unitTestRunner": {
"id": "karma",
"displayName": "Karma"
},
"testFramework": {
"id": "jasmine",
"displayName": "Jasmine"
},
"editor": {
"id": "vscode",
"displayName": "Visual Studio Code"
},
"platform": {
"id": "default",
"displayName": "Default"
}
};
/***********************************************************************************
* code that is needed to generate project from options above
*/
const path = './node_modules/aurelia-cli/lib/';
const ProjectTemplate = require(path + 'commands/new/project-template').ProjectTemplate;
const transform = require(path + 'colors/transform');
const CLIOptions = require(path + 'cli-options').CLIOptions;
options = {
hasFlag: function () {
return false
}
};
var project = new ProjectTemplate(model, options);
var configurePlatform = require(path + `commands/new/platforms/${model.platform.id}`);
configurePlatform(project, options);
var configureTranspiler = require(path + `commands/new/transpilers/${model.transpiler.id}`);
configureTranspiler(project, options);
var configureMarkupProcessor = require(path + `commands/new/markup-processors/${model.markupProcessor.id}`);
configureMarkupProcessor(project, options);
var configureCSSProcessor = require(path + `commands/new/css-processors/${model.cssProcessor.id}`);
configureCSSProcessor(project, options);
var configureUnitTestRunner = require(path + `commands/new/unit-test-runners/${model.unitTestRunner.id}`);
configureUnitTestRunner(project, options);
var configureEditor = require(path + `commands/new/editors/${model.editor.id}`);
configureEditor(project, options);
project.create({}, pathToAddProject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment