forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schematics/search): find and install schematics
- Loading branch information
Wassim CHEGHAM
committed
Mar 23, 2018
1 parent
0f7a818
commit 6efa6c3
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// import { oneLine } from 'common-tags'; | ||
import { CliConfig } from '../models/config'; | ||
|
||
const Command = require('../ember-cli/lib/models/command'); | ||
|
||
|
||
export default Command.extend({ | ||
name: 'schematics', | ||
aliases: ['s'], | ||
description: 'Search or install Schematics.', | ||
works: 'insideProject', | ||
availableOptions: [ | ||
{ | ||
name: 'search', | ||
type: String, | ||
default: false, | ||
description: 'search Schematics' | ||
}, | ||
{ | ||
name: 'install', | ||
type: String, | ||
default: false, | ||
description: 'Install Schematics' | ||
} | ||
], | ||
run: function (commandOptions: any) { | ||
const SchematicSearchTask = require('../tasks/schematic-search').default; | ||
|
||
const schematicSearchTask = new SchematicSearchTask({ | ||
ui: this.ui, | ||
project: this.project | ||
}); | ||
|
||
return schematicSearchTask.run({ | ||
...commandOptions, | ||
configs: CliConfig.fromProject().config.lint | ||
}); | ||
} | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const Task = require('../ember-cli/lib/models/task'); | ||
import chalk from 'chalk'; | ||
import { spawn } from 'child_process'; | ||
const inquirer = require('inquirer'); | ||
inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt')); | ||
|
||
const algoliasearch = require('algoliasearch'); | ||
const algolia = { | ||
appId: 'OFCNCOG2CU', | ||
apiKey: '6fe4476ee5a1832882e326b506d14126', | ||
indexName: 'npm-search' | ||
}; | ||
|
||
const client = algoliasearch(algolia.appId, algolia.apiKey).initIndex(algolia.indexName); | ||
const alsearch = async (answers: any, query: string) => { | ||
const res = await client.search(`angular+schematics+${query}`, { | ||
attributesToRetrieve: ['name', 'version', 'description', 'owner', 'humanDownloadsLast30Days'], | ||
offset: 0, | ||
length: 4 | ||
}); | ||
return res.hits.map(p => `${p.name}`); | ||
}; | ||
|
||
function displayAnswers() { | ||
return inquirer | ||
.prompt([ | ||
{ | ||
type: 'checkbox-plus', | ||
name: 'schematics', | ||
message: '🔎 Find and install Angular CLI Schematics:', | ||
pageSize: 10, | ||
highlight: true, | ||
searchable: true, | ||
source: alsearch | ||
} | ||
]) | ||
.then(answers => answers.schematics); | ||
} | ||
|
||
export default Task.extend({ | ||
run: async function() { | ||
const ui = this.ui; | ||
let packageManager = 'npm'; | ||
ui.writeLine(chalk.green(`Installing Schematics via ${packageManager}.`)); | ||
|
||
const installOptions = { | ||
stdio: 'inherit', | ||
shell: true | ||
}; | ||
|
||
const schematics: string[] = await displayAnswers(); | ||
|
||
// tslint:disable-next-line:max-line-length | ||
const installArgs: string[] = ['install', '--dry-run', '--loglevel=error', '--silent', ...schematics]; | ||
return new Promise((resolve, reject) => { | ||
spawn(packageManager, installArgs, installOptions).on('close', (code: number) => { | ||
if (code === 0) { | ||
ui.writeLine(chalk.green(`Successfully installed new Schematics.`)); | ||
resolve(); | ||
} else { | ||
const message = 'Failed installed new Schematics, see above.'; | ||
ui.writeLine(chalk.red(message)); | ||
reject(message); | ||
} | ||
}); | ||
}); | ||
} | ||
}); |