Skip to content

Commit

Permalink
feat(schematics/search): find and install schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
Wassim CHEGHAM committed Mar 23, 2018
1 parent 0f7a818 commit 6efa6c3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@angular-devkit/build-optimizer": "~0.0.28",
"@angular-devkit/schematics": "~0.0.35",
"@schematics/angular": "~0.1.0",
"algoliasearch": "^3.26.0",
"autoprefixer": "^6.5.3",
"chalk": "~2.2.0",
"circular-dependency-plugin": "^3.0.0",
Expand All @@ -61,6 +62,8 @@
"fs-extra": "^4.0.0",
"glob": "^7.0.3",
"html-webpack-plugin": "^2.29.0",
"inquirer": "^5.1.0",
"inquirer-checkbox-plus-prompt": "^1.0.1",
"istanbul-instrumenter-loader": "^2.0.0",
"karma-source-map-support": "^1.2.0",
"less": "^2.7.2",
Expand Down
39 changes: 39 additions & 0 deletions packages/@angular/cli/commands/schematics.ts
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
});
}
});
1 change: 1 addition & 0 deletions packages/@angular/cli/lib/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function loadCommands() {
'completion': require('../../commands/completion').default,
'doc': require('../../commands/doc').default,
'xi18n': require('../../commands/xi18n').default,
'schematics': require('../../commands/schematics').default,

// Easter eggs.
'make-this-awesome': require('../../commands/easter-egg').default,
Expand Down
68 changes: 68 additions & 0 deletions packages/@angular/cli/tasks/schematic-search.ts
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);
}
});
});
}
});

0 comments on commit 6efa6c3

Please sign in to comment.