Last active
July 3, 2016 19:08
-
-
Save vegarringdal/161999c86be0383f0b06ae48daa2558e to your computer and use it in GitHub Desktop.
aurelia-cli generate project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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(); | |
} | |
}); | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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") | |
}) | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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