ng-matero
Version:
Angular Material admin template
209 lines • 10.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const schematics_2 = require("@angular/cdk/schematics");
const project_targets_1 = require("@schematics/angular/utility/project-targets");
const workspace_1 = require("@schematics/angular/utility/workspace");
const jsonc_parser_1 = require("jsonc-parser");
const global_loader_1 = require("./global-loader");
const material_fonts_1 = require("./material-fonts");
const package_config_1 = require("./package-config");
const packages_1 = require("./packages");
/**
* Scaffolds the basics of a Angular Material application, this includes:
* - Add Starter files to root
* - Add Scripts to `package.json`
* - Add proxy to `angular.json`
* - Add fileReplacements to `angular.json`
* - Add paths to `tsconfig.json`
* - Add Fonts & Icons to `index.html`
* - Add Preloader to `index.html`
* - Add Packages to `package.json`
*/
function default_1(options) {
return (0, schematics_1.chain)([
deleteExsitingFiles(options),
addStarterFiles(options),
addScriptsToPackageJson(),
addESLintToAngularJson(options),
addProxyToAngularJson(options),
addFileReplacementsToAngularJson(options),
addPathsToTsconfig(options),
(0, material_fonts_1.addFontsToIndex)(options),
(0, global_loader_1.addLoaderToIndex)(options),
installPackages(),
]);
}
/** delete exsiting files to be overwrite */
function deleteExsitingFiles(options) {
return (host) => __awaiter(this, void 0, void 0, function* () {
const workspace = yield (0, workspace_1.getWorkspace)(host);
const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
[
`${project.sourceRoot}/app/app-routing-module.ts`,
`${project.sourceRoot}/app/app-module.ts`,
`${project.sourceRoot}/app/app.config.ts`,
`${project.sourceRoot}/app/app.routes.ts`,
`${project.sourceRoot}/app/app.spec.ts`,
`${project.sourceRoot}/app/app.ts`,
`${project.sourceRoot}/app/app.html`,
`${project.sourceRoot}/app/app.scss`,
`${project.sourceRoot}/environments/environment.prod.ts`,
`${project.sourceRoot}/environments/environment.ts`,
`${project.sourceRoot}/styles.scss`,
]
.filter(p => host.exists(p))
.forEach(p => host.delete(p));
});
}
/** Add scripts to `package.json` */
function addScriptsToPackageJson() {
return (host) => {
(0, package_config_1.addScriptToPackageJson)(host, 'build:prod', 'ng build --prod');
(0, package_config_1.addScriptToPackageJson)(host, 'lint', `ng lint --fix && npm run lint:scss`);
(0, package_config_1.addScriptToPackageJson)(host, 'lint:ts', `eslint "src/**/*.ts" --fix`);
(0, package_config_1.addScriptToPackageJson)(host, 'lint:scss', `stylelint "src/**/*.scss" --fix`);
};
}
/** Add ESLint to `angular.json` */
function addESLintToAngularJson(options) {
return (0, workspace_1.updateWorkspace)(workspace => {
const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
// Default Angular CLI project at the root of the workspace
const lintFilePatternsRoot = project.root === '' ? 'src' : project.root;
const eslintTargetConfig = {
builder: '@angular-eslint/builder:lint',
options: {
lintFilePatterns: [`${lintFilePatternsRoot}/**/*.ts`, `${lintFilePatternsRoot}/**/*.html`],
},
};
project.targets.set('lint', eslintTargetConfig);
});
}
/** Add proxy to `angular.json` */
function addProxyToAngularJson(options) {
return (0, workspace_1.updateWorkspace)(workspace => {
const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
const targetServeConfig = project.targets.get('serve');
if (targetServeConfig.options) {
targetServeConfig.options.buildTarget = `${options.project}:build`;
targetServeConfig.options.proxyConfig = 'proxy.config.js';
}
else {
targetServeConfig.options = {
buildTarget: `${options.project}:build`,
proxyConfig: 'proxy.config.js',
};
}
});
}
/** Add fileReplacements to 'angular.json' */
function addFileReplacementsToAngularJson(options) {
return (0, workspace_1.updateWorkspace)(workspace => {
const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
const targetBuildConfig = project.targets.get('build');
// The previous environment file has been deleted
const replace = {
replace: 'src/environments/environment.ts',
with: 'src/environments/environment.prod.ts',
};
const production = targetBuildConfig.configurations.production;
// Add file replacement option entry for the configuration environment file
const replacements = production.fileReplacements;
if (replacements === undefined) {
production.fileReplacements = [replace];
}
else {
const existing = replacements.find(value => value.replace === replace.replace);
if (existing) {
if (existing.with !== replace.with) {
existing.with = replace.with;
}
}
else {
replacements.push(replace);
}
}
});
}
/** Add paths to `tsconfig.json` */
function addPathsToTsconfig(options) {
return (host) => __awaiter(this, void 0, void 0, function* () {
const workspace = yield (0, workspace_1.getWorkspace)(host);
const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
const fileName = 'tsconfig.json';
const formattingOptions = { insertSpaces: true, tabSize: 2 };
let fileContent = host.read(fileName).toString();
let edits = (0, jsonc_parser_1.modify)(fileContent, ['compilerOptions', 'baseUrl'], './', {
formattingOptions,
});
fileContent = (0, jsonc_parser_1.applyEdits)(fileContent, edits);
const currentJson = (0, jsonc_parser_1.parse)(fileContent);
const currentPaths = currentJson.compilerOptions.paths || {};
const pathsToUpdate = {
'@core': `${project.sourceRoot}/app/core`,
'@core/*': `${project.sourceRoot}/app/core/*`,
'@shared': `${project.sourceRoot}/app/shared`,
'@shared/*': `${project.sourceRoot}/app/shared/*`,
'@theme': `${project.sourceRoot}/app/theme`,
'@theme/*': `${project.sourceRoot}/app/theme/*`,
'@env': `${project.sourceRoot}/environments`,
'@env/*': `${project.sourceRoot}/environments/*`,
};
Object.keys(pathsToUpdate).forEach(key => {
const newPath = pathsToUpdate[key];
const existingPaths = Array.isArray(currentPaths[key]) ? currentPaths[key] : [];
const updatedPaths = Array.from(new Set([...existingPaths, newPath]));
edits = (0, jsonc_parser_1.modify)(fileContent, ['compilerOptions', 'paths', key], updatedPaths, {
formattingOptions,
});
fileContent = (0, jsonc_parser_1.applyEdits)(fileContent, edits);
});
host.overwrite(fileName, fileContent);
});
}
/** Add starter files to root */
function addStarterFiles(options) {
return (host) => __awaiter(this, void 0, void 0, function* () {
const workspace = yield (0, workspace_1.getWorkspace)(host);
const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
const mainFilePath = (0, schematics_2.getProjectMainFile)(project);
options.zoneless = (0, project_targets_1.isZonelessApp)(project);
return (0, schematics_1.chain)([
(0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/root-files'), [
(0, schematics_1.template)(Object.assign(Object.assign({}, core_1.strings), options)),
]), schematics_1.MergeStrategy.Overwrite),
(0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/common-files'), [
(0, schematics_1.template)(Object.assign(Object.assign({}, core_1.strings), options)),
(0, schematics_1.move)(project.root),
]), schematics_1.MergeStrategy.Overwrite),
(0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)((0, schematics_2.isStandaloneApp)(host, mainFilePath)
? './files/standalone-files'
: './files/module-files'), [
(0, schematics_1.template)(Object.assign(Object.assign({}, core_1.strings), options)),
(0, schematics_1.move)(project.sourceRoot),
]), schematics_1.MergeStrategy.Overwrite),
]);
});
}
/** Install packages */
function installPackages() {
return (host, context) => {
// Add 3rd packages
(0, packages_1.add3rdPkgsToPackageJson)(host);
context.addTask(new tasks_1.NodePackageInstallTask());
};
}
//# sourceMappingURL=setup-project.js.map