UNPKG

5.57 kBJavaScriptView Raw
1"use strict";
2
3const fs = require("fs");
4const path = require("path");
5var program = require("commander");
6
7const solutionDir = path.join(__dirname, "..");
8
9const resolvePathToAbsolute = (baseDir, targetPath) => {
10 if (path.isAbsolute(targetPath)) return targetPath;
11 else if (baseDir) return path.join(baseDir, targetPath);
12 else
13 throw new Error(
14 "Can't resolve relative path without base dir: " + relativePath
15 );
16};
17
18const get_mapping = (partsFile, assetsFile, mappingTemplate) => {
19 const result = [];
20 partsFile.forEach(f => {
21 const partsProvider = require(`../${f}`);
22 const part = partsProvider();
23 part.entries.forEach(e => {
24 const sourceDirectory = path.join(path.dirname(f), e.outputPath);
25 const fileName = `${e.name}.js`;
26 result.push({
27 sourceFile: path.join(sourceDirectory, fileName),
28 destinationFile: path.join(
29 mappingTemplate.parts[part.name].entriesDist,
30 fileName
31 )
32 });
33 });
34
35 if (part.commonChunk !== undefined) {
36 const fileName = `${path.parse(part.commonChunk).base}.js`;
37 result.push({
38 sourceFile: path.join(
39 path.dirname(f),
40 path.dirname(part.commonChunk),
41 fileName
42 ),
43 destinationFile: path.join(
44 mappingTemplate.parts[part.name].commonChunkDist,
45 fileName
46 )
47 });
48 }
49
50 if (part.libraries !== undefined) {
51 part.libraries.forEach(l => {
52 const sourceDirectory = path.join(
53 path.dirname(f),
54 l.outputPath
55 );
56 const fileName = `${l.name}.js`;
57 result.push({
58 sourceFile: path.join(sourceDirectory, fileName),
59 destinationFile: path.join(
60 mappingTemplate.parts[part.name].librariesDist,
61 fileName
62 )
63 });
64 });
65 }
66 });
67 assetsFile.forEach(f => {
68 const assetsProvider = require(`../${f}`);
69 const assets = assetsProvider();
70 assets.forEach(a => {
71 const sourceDirectory = path.join(
72 path.dirname(f),
73 a.destinationDirectory
74 );
75 a.sourceFiles.forEach(s => {
76 const fileName = path.basename(s);
77 const sourceFile = path.join(sourceDirectory, fileName);
78
79 if (!mappingTemplate.assets[a.name])
80 throw Error(`Asset not found`);
81
82 result.push({
83 sourceFile: sourceFile,
84 destinationFile: path.join(
85 mappingTemplate.assets[a.name].destinationDirectory,
86 fileName
87 )
88 });
89 });
90 });
91 });
92
93 const sourceDirectory = path.join("dist", "content", "localization");
94 fs.readdirSync(sourceDirectory).forEach(f => {
95 const fileName = path.parse(f).base;
96 result.push({
97 sourceFile: path.join(sourceDirectory, f),
98 destinationFile: path.join(
99 mappingTemplate.localizationDist,
100 fileName
101 )
102 });
103 });
104
105 return result;
106};
107
108const generate_reverse_mapping = (templatePath, projectName) => {
109 if (!program.template) throw Error("Template is not specified");
110
111 const mappingTemplate = require(resolvePathToAbsolute(
112 solutionDir,
113 templatePath
114 ));
115 const projects = require("./projects.json");
116 const profiles = {};
117
118 if (projectName && !projects.parts[projectName])
119 throw Error(`Profile ${projectName} is not found`);
120
121 for (const property in projects.parts) {
122 if (!projectName || property == projectName) {
123 const parts = projects.parts[property];
124 profiles[property] = { parts: parts, assets: [] };
125 }
126 }
127
128 for (const property in projects.assets) {
129 const assets = projects.assets[property];
130 if (!projectName || (assets && property == projectName)) {
131 if (profiles[property] !== undefined) {
132 profiles[property] = {
133 parts: profiles[property].parts,
134 assets: assets
135 };
136 } else {
137 profiles[property] = { parts: [], assets: assets };
138 }
139 }
140 }
141
142 for (const profile in profiles) {
143 profiles[profile] = get_mapping(
144 profiles[profile].parts,
145 profiles[profile].assets,
146 mappingTemplate
147 );
148 }
149
150 fs.writeFileSync(
151 path.join(solutionDir, "directcrm-modules-map.json"),
152 JSON.stringify(profiles, {}, 4)
153 );
154};
155
156program
157 .option("-t, --template <file>", "Template file path")
158 .option(
159 "-p, --profile [name]",
160 "Installation profile name, can be one of the following: `tracker`, `administration`, `directCrm`, `nexus` etc." +
161 "By default inject mapping would be created for all profiles from project.json"
162 )
163 .parse(process.argv);
164
165generate_reverse_mapping(program.template, program.profile);