ng-afelio
Version:
Extended Angular CLI
228 lines (227 loc) • 8.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
function installDependencies() {
return (host, context) => {
const toInstall = [];
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/addon-actions',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/addon-essentials',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/addon-links',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/angular',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/builder-webpack5',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/manager-webpack5',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/addons',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/addon-controls',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@storybook/theming',
version: '6.5.9',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: '@compodoc/compodoc',
version: '1.1.19',
overwrite: true,
});
toInstall.push({
type: dependencies_1.NodeDependencyType.Default,
name: 'ngx-translate-multi-http-loader',
version: '7.0.5',
overwrite: false,
});
toInstall.forEach(dep => {
dependencies_1.addPackageJsonDependency(host, dep);
});
context.addTask(new tasks_1.NodePackageInstallTask(), []);
};
}
function updateScripts() {
return (host) => {
const packageFile = '/package.json';
const text = host.read(packageFile);
if (!text) {
throw new schematics_1.SchematicsException(`Can not find "package.json" file in your project.`);
}
const sourceText = text.toString('utf8');
const toInsert = `
"build-ds": "ng-afelio build design-system"
`;
const alreadyInstalled = sourceText.indexOf(toInsert) !== -1;
if (!alreadyInstalled) {
const toReplaceMatch = sourceText.match(/"start": "(.+)"/);
if (toReplaceMatch) {
const jsonContent = JSON.parse(sourceText);
jsonContent.scripts['build-ds'] = 'ng-afelio build design-system';
jsonContent.scripts['docs:json'] = 'compodoc -p ./tsconfig.json -e json -d .';
jsonContent.scripts['docs'] = 'compodoc -p ./tsconfig.json . -s';
jsonContent.scripts['storybook'] = 'npm run docs:json && start-storybook -p 6007';
jsonContent.scripts['build-storybook'] = 'npm run docs:json && build-storybook';
host.overwrite(packageFile, JSON.stringify(jsonContent, null, 2));
}
}
return host;
};
}
function updateTsconfigAlias(optionName) {
return (host) => {
const tsconfigFile = '/tsconfig.json';
const text = host.read(tsconfigFile);
if (!text) {
throw new schematics_1.SchematicsException(`Can not find "tsconfig.json" file in your project.`);
}
let sourceText = text.toString('utf8');
sourceText = sourceText.replace(/\/\*.*\*\//gm, '');
const jsonContent = JSON.parse(sourceText);
const pathExist = !!jsonContent.compilerOptions.paths;
if (pathExist) {
jsonContent.compilerOptions.paths[`${optionName}`] = [`projects/${optionName}/src/public-api.ts`];
}
else {
jsonContent.compilerOptions.paths = {};
jsonContent.compilerOptions.paths[`${optionName}`] = [`projects/${optionName}/src/public-api.ts`];
}
host.overwrite(tsconfigFile, JSON.stringify(jsonContent, null, 2));
return host;
};
}
function addStorybookConfigToAngularJSON() {
const storybookConfig = {
'root': '',
'projectType': 'library',
'architect': {
'build': {
'builder': '@angular-devkit/build-angular:browser',
'options': {
'assets': [
'src/favicon.ico',
'src/assets',
{
'input': 'projects/ui-kit/src/assets',
'glob': '**/*',
'output': './assets',
},
],
'styles': [
'src/styles.scss',
],
},
},
},
};
return (host) => {
const angularFile = '/angular.json';
const text = host.read(angularFile);
if (!text) {
throw new schematics_1.SchematicsException(`Can not find "angular.json" file in your project.`);
}
const sourceText = text.toString('utf8');
const jsonContent = JSON.parse(sourceText);
jsonContent.projects['storybook'] = storybookConfig;
host.overwrite(angularFile, JSON.stringify(jsonContent, null, 2));
return host;
};
}
function installNgxTranslate() {
return (host, context) => {
const packageFile = '/package.json';
const text = host.read(packageFile);
if (!text) {
throw new schematics_1.SchematicsException(`Can not find "package.json" file in your project.`);
}
const sourceText = text.toString('utf8');
const alreadyInstalled = sourceText.indexOf(`"@ngx-translate/core"`) !== -1;
if (!alreadyInstalled) {
return schematics_1.schematic('install-translate', {})(host, context);
}
return host;
};
}
async function applyDesignSytemTemplate(options) {
const projectDesignSytemPath = `/projects/${options.name}/src`;
const templateSource = schematics_1.apply(schematics_1.url('./files'), [
schematics_1.template({
...core_1.strings,
...options,
}),
schematics_1.move(projectDesignSytemPath),
]);
return schematics_1.mergeWith(templateSource, schematics_1.MergeStrategy.Overwrite);
}
async function applyDesignSystemDocumentationTemplate(options) {
const projectDesignSytemPath = `./`;
const templateSource = schematics_1.apply(schematics_1.url('./storybook-files'), [
schematics_1.template({
...core_1.strings,
...options,
}),
schematics_1.move(projectDesignSytemPath),
]);
return schematics_1.mergeWith(templateSource, schematics_1.MergeStrategy.Overwrite);
}
function default_1(options) {
const name = options.name ? options.name : 'design-system';
options.name = name;
const prefix = options.prefix ? options.prefix : 'ds';
options.prefix = prefix;
return async () => {
return schematics_1.chain([
schematics_1.externalSchematic('@schematics/angular', 'library', {
name,
prefix,
skipInstall: true,
}),
installNgxTranslate(),
await applyDesignSytemTemplate(options),
await applyDesignSystemDocumentationTemplate(options),
addStorybookConfigToAngularJSON(),
updateScripts(),
updateTsconfigAlias(name),
installDependencies(),
]);
};
}
exports.default = default_1;