forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schematic-search.ts
68 lines (61 loc) · 2.05 KB
/
schematic-search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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);
}
});
});
}
});