@nstudio/schematics
Version:
Cross-platform (xplat) tools for Nx workspaces.
203 lines (198 loc) • 9.81 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const utils_1 = require("../utils");
function default_1(options) {
if (!options.name) {
throw new schematics_1.SchematicsException(utils_1.missingArgument('name', 'Provide a name for your Electron app.', 'ng g app.electron sample'));
}
if (!options.target) {
throw new schematics_1.SchematicsException(`Missing target argument. Provide the name of the web app in your workspace to use inside the electron app. ie, web-myapp`);
}
return schematics_1.chain([
utils_1.prerun(options),
// adjust naming convention
utils_1.applyAppNamingConvention(options, 'electron'),
// create app files
(tree, context) => addAppFiles(options, options.name)(tree, context),
// add root package dependencies
(tree) => utils_1.addRootDeps(tree, { electron: true }),
// add npm scripts
(tree) => {
const platformApp = options.name.replace('-', '.');
let fullTargetAppName = options.target;
let targetAppScript = fullTargetAppName.replace('-', '.');
const packageConfig = utils_1.getJsonFromFile(tree, "package.json");
const scripts = packageConfig.scripts || {};
const postinstall = 'electron-rebuild install-app-deps';
if (scripts.postinstall) {
// add to the end of already existing postinstall
scripts['postinstall'] = `${scripts.postinstall} && ${postinstall}`;
}
else {
scripts['postinstall'] = postinstall;
}
scripts['postinstall.electron'] = 'node tools/electron/postinstall';
scripts['postinstall.web'] = 'node tools/web/postinstall';
scripts[`build.${platformApp}`] = `npm run prepare.${platformApp} && ng build ${options.name} --prod --base-href ./`;
scripts[`build.${platformApp}.local`] = `npm run build.${platformApp} && electron dist/apps/${options.name}`;
scripts[`build.${platformApp}.linux`] = `npm run build.${platformApp} && cd dist/apps/${options.name} && npx electron-builder build --linux`;
scripts[`build.${platformApp}.windows`] = `npm run build.${platformApp} && cd dist/apps/${options.name} && npx electron-builder build --windows`;
scripts[`build.${platformApp}.mac`] = `npm run build.${platformApp} && cd dist/apps/${options.name} && npx electron-builder build --mac`;
scripts[`prepare.${platformApp}`] = `npm run postinstall.electron && tsc -p apps/${options.name}/tsconfig.json`;
scripts[`serve.${platformApp}.target`] = `ng serve ${options.name}`;
scripts[`serve.${platformApp}`] = `wait-on http-get://localhost:4200/ && electron apps/${options.name}/src --serve`;
scripts[`start.${platformApp}`] = `npm run prepare.${platformApp} && npm-run-all -p serve.${platformApp}.target serve.${platformApp}`;
// adjust web related scripts to account for postinstall hooks
const startWeb = scripts[`start.${targetAppScript}`];
const postinstallWeb = 'npm run postinstall.web';
if (startWeb) {
// prefix it
scripts[`start.${targetAppScript}`] = `${postinstallWeb} && ${startWeb}`;
}
else {
// create to be consistent
scripts[`start.${targetAppScript}`] = `${postinstallWeb} && ng serve ${fullTargetAppName}`;
}
let startDefault = scripts[`start`];
if (startDefault) {
// prefix it
if (startDefault.indexOf(fullTargetAppName) === -1) {
// set target app as default
startDefault = `${startDefault} ${fullTargetAppName}`;
}
scripts[`start`] = `${postinstallWeb} && ${startDefault}`;
}
else {
scripts[`start`] = `${postinstallWeb} && ng serve ${fullTargetAppName}`;
}
let buildDefault = scripts[`build`];
if (buildDefault) {
// prefix it
if (buildDefault.indexOf(fullTargetAppName) === -1) {
// set target app as default
buildDefault = `${buildDefault} ${fullTargetAppName}`;
}
scripts[`build`] = `${postinstallWeb} && ${buildDefault}`;
}
else {
scripts[`build`] = `${postinstallWeb} && ng build ${fullTargetAppName}`;
}
let testDefault = scripts[`test`];
if (testDefault) {
// prefix it
scripts[`test`] = `${postinstallWeb} && ${testDefault}`;
}
else {
scripts[`test`] = `${postinstallWeb} && ng test`;
}
let e2eDefault = scripts[`e2e`];
if (e2eDefault) {
// prefix it
scripts[`e2e`] = `${postinstallWeb} && ${e2eDefault}`;
}
else {
scripts[`e2e`] = `${postinstallWeb} && ng e2e`;
}
return utils_1.updatePackageScripts(tree, scripts);
},
// angular.json
(tree) => {
// grab the target app configuration
const ngConfig = utils_1.getJsonFromFile(tree, "angular.json");
// find app
const fullTargetAppName = options.target;
let targetConfig;
if (ngConfig && ngConfig.projects) {
targetConfig = ngConfig.projects[fullTargetAppName];
}
if (!targetConfig) {
throw new schematics_1.SchematicsException(`The target app name "${fullTargetAppName}" does not appear to be in your workspace angular.json. You may need to generate it first or perhaps check the spelling.`);
}
const projects = {};
const electronAppName = options.name;
projects[electronAppName] = targetConfig;
// update to use electron module
projects[electronAppName].architect.build.options.outputPath = `dist/apps/${electronAppName}`;
projects[electronAppName].architect.build.options.main = `apps/${fullTargetAppName}/src/main.electron.ts`;
projects[electronAppName].architect.build.options.assets.push({
"glob": "**/*",
"input": `apps/${electronAppName}/src/`,
"ignore": [
"**/*.ts"
],
"output": ""
});
projects[electronAppName].architect.serve.options.browserTarget = `${electronAppName}:build`;
projects[electronAppName].architect.serve.configurations.production.browserTarget = `${electronAppName}:build:production`;
// clear other settings (TODO: may need these in future), for now keep electron options minimal
delete projects[electronAppName].architect['extract-i18n'];
delete projects[electronAppName].architect['test'];
delete projects[electronAppName].architect['lint'];
return utils_1.updateAngularProjects(tree, projects);
},
// nx.json
(tree) => {
const projects = {};
projects[`${options.name}`] = {
tags: []
};
return utils_1.updateNxProjects(tree, projects);
},
// adjust app files
(tree) => adjustAppFiles(options, tree),
// add tooling
utils_1.addPostinstallers(),
options.skipFormat
? schematics_1.noop()
: utils_1.formatFiles(options)
]);
}
exports.default = default_1;
function addAppFiles(options, appPath, sample = '') {
sample = '';
const appname = utils_1.getAppName(options, 'electron');
return schematics_1.branchAndMerge(schematics_1.mergeWith(schematics_1.apply(schematics_1.url(`./_${sample}files`), [
schematics_1.template(Object.assign({}, options, { appname, utils: utils_1.stringUtils, npmScope: utils_1.getNpmScope(), prefix: utils_1.getPrefix(), dot: '.' })),
schematics_1.move(`apps/${appPath}`)
])));
}
function adjustAppFiles(options, tree) {
const fullTargetAppName = options.target;
const electronModulePath = `/apps/${fullTargetAppName}/src/app/app.electron.module.ts`;
if (!tree.exists(electronModulePath)) {
tree.create(electronModulePath, electronModule());
}
const electronMainPath = `/apps/${fullTargetAppName}/src/main.electron.ts`;
if (!tree.exists(electronMainPath)) {
tree.create(electronMainPath, electronMain());
}
return tree;
}
function electronModule() {
return `import { NgModule } from '@angular/core';
import { ${utils_1.stringUtils.classify(utils_1.getPrefix())}ElectronCoreModule } from '@${utils_1.getNpmScope()}/electron';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
@NgModule({
imports: [AppModule, ${utils_1.stringUtils.classify(utils_1.getPrefix())}ElectronCoreModule],
bootstrap: [AppComponent]
})
export class AppElectronModule {}`;
}
function electronMain() {
return `import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
// libs
import { environment } from '@${utils_1.getNpmScope()}/core';
// app
import { AppElectronModule } from './app/app.electron.module';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic()
.bootstrapModule(AppElectronModule)
.catch(err => console.log(err));
`;
}
//# sourceMappingURL=index.js.map
;