UNPKG

5.43 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var path = require("path");
4var utils_1 = require("./utils");
5var typescript_1 = require("typescript");
6function parseConfigFile(filePath) {
7 var _a = typescript_1.readConfigFile(filePath, utils_1.readFile), config = _a.config, error = _a.error;
8 if (utils_1.isNil(error)) {
9 if (!utils_1.isNil(config.extends)) {
10 var extendFrom = config.extends;
11 var baseConfigPath = path.resolve(path.dirname(filePath), extendFrom);
12 if (utils_1.isFile(baseConfigPath) && baseConfigPath !== filePath) {
13 var baseConfigs = parseConfigFile(baseConfigPath);
14 if (utils_1.isNil(baseConfigs.error)) {
15 delete config.extends;
16 var extendedConfigs = utils_1.mergeInto(config, baseConfigs.configs);
17 return {
18 configs: extendedConfigs,
19 error: undefined,
20 };
21 }
22 }
23 }
24 }
25 return {
26 configs: config,
27 error: error,
28 };
29}
30exports.parseConfigFile = parseConfigFile;
31function getSafePackageName(name) {
32 if (/^(?:[a-z][a-z0-9._-]+?|@[^\/]*\/\b)+$/.test(name) === false) {
33 return undefined;
34 }
35 return name.replace(/[^\w-]/g, '-').replace(/^-+/, '');
36}
37exports.getSafePackageName = getSafePackageName;
38function createNGCConfig(filePath, moduleId, configs) {
39 var result = utils_1.mergeInto({
40 compilerOptions: {
41 outDir: './ngc-compiled',
42 baseUrl: '.',
43 target: 'es2015',
44 module: 'es2015',
45 sourceMap: false,
46 declaration: true,
47 lib: [
48 'es2017',
49 'dom',
50 ],
51 },
52 angularCompilerOptions: {
53 strictMetadataEmit: true,
54 skipTemplateCodegen: true,
55 annotateForClosureCompiler: true,
56 flatModuleOutFile: moduleId + '.js',
57 },
58 }, configs);
59 if (!utils_1.isNil(result.angularCompilerOptions) && utils_1.isNil(result.angularCompilerOptions.flatModuleId)) {
60 result.angularCompilerOptions.flatModuleId = moduleId;
61 }
62 if (!utils_1.isNil(result.include)) {
63 delete result.include;
64 }
65 if (!utils_1.isNil(result.exclude)) {
66 delete result.exclude;
67 }
68 if (utils_1.isNil(result.files)) {
69 var _a = result.bundlerOptions, _b = (_a === void 0 ? {} : _a).entry, entry = _b === void 0 ? '' : _b;
70 result.files = [entry];
71 }
72 if (!utils_1.isNil(result.bundlerOptions)) {
73 delete result.bundlerOptions;
74 }
75 var resultString = JSON.stringify(result);
76 utils_1.writeFile(filePath, resultString);
77}
78exports.createNGCConfig = createNGCConfig;
79function readPackage(packageFilePath) {
80 var content = utils_1.readFile(packageFilePath);
81 try {
82 return JSON.parse(content);
83 }
84 catch (_err) {
85 return {};
86 }
87}
88exports.readPackage = readPackage;
89function isPkgModuleEntryValid(value, entry, projectPath) {
90 value = path.resolve(projectPath, value);
91 entry = path.resolve(projectPath, entry);
92 return value.toLowerCase() === entry.toLowerCase();
93}
94function validatePkgModuleEntries(_a, pkg, projectPath) {
95 var pkgMain = _a.pkgMain, pkgModule = _a.pkgModule, pkgES2015 = _a.pkgES2015, pkgTypings = _a.pkgTypings;
96 if (!utils_1.isNil(pkgMain)) {
97 if (utils_1.isNil(pkg.main) || !isPkgModuleEntryValid(pkgMain, pkg.main, projectPath)) {
98 console.warn("Warning: You must set \"main\" property of your package.json to \"" + pkgMain.replace(projectPath + path.sep, '') + "\"");
99 }
100 }
101 if (!utils_1.isNil(pkgModule)) {
102 if (utils_1.isNil(pkg.module) || !isPkgModuleEntryValid(pkgModule, pkg.module, projectPath)) {
103 console.warn("Warning: You must set \"module\" property of your package.json to \"" + pkgModule.replace(projectPath + path.sep, '') + "\"");
104 }
105 }
106 if (!utils_1.isNil(pkgES2015)) {
107 if (utils_1.isNil(pkg.es2015) || !isPkgModuleEntryValid(pkgES2015, pkg.es2015, projectPath)) {
108 console.warn("Warning: You must set \"es2015\" property of your package.json to \"" + pkgES2015.replace(projectPath + path.sep, '') + "\"");
109 }
110 }
111 if (!utils_1.isNil(pkgTypings)) {
112 if (utils_1.isNil(pkg.typings) || !isPkgModuleEntryValid(pkgTypings, pkg.typings, projectPath)) {
113 console.warn("Warning: You must set \"typings\" property of your package.json to \"" + pkgTypings.replace(projectPath + path.sep, '') + "\"");
114 }
115 }
116}
117exports.validatePkgModuleEntries = validatePkgModuleEntries;
118function validatePkgDependencies(pkg, externalModules) {
119 var _a = pkg.dependencies, dependencies = _a === void 0 ? {} : _a, _b = pkg.peerDependencies, peerDependencies = _b === void 0 ? {} : _b;
120 if (!utils_1.isEmpty(externalModules)) {
121 for (var _i = 0, externalModules_1 = externalModules; _i < externalModules_1.length; _i++) {
122 var moduleName = externalModules_1[_i];
123 if (utils_1.isNil(dependencies[moduleName]) && utils_1.isNil(peerDependencies[moduleName])) {
124 console.warn("Warning: You must add \"" + moduleName + "\" into \"dependencies\" of your package.json");
125 }
126 }
127 }
128}
129exports.validatePkgDependencies = validatePkgDependencies;