mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
refactor: adjust code to new configuration
This commit is contained in:
137
src/pack.ts
137
src/pack.ts
@@ -72,18 +72,15 @@ async function packCliJs(): Promise<void> {
|
||||
|
||||
let internalRequire: string | null = null;
|
||||
|
||||
const adjustedContent = '#!/usr/bin/env node\n\n' + content
|
||||
const adjustedContent = content
|
||||
.split('\n')
|
||||
.map((line, lineNumber) => {
|
||||
|
||||
// check for exports (cli.js is not allowed to export anything)
|
||||
if (Array.isArray(line.match(/^\s*((exports)|(module\.exports))/))) {
|
||||
throw new Error(
|
||||
'Line ' +
|
||||
lineNumber +
|
||||
' in cli.js has a reference to the exports object. cli.js is not for exporting. Line was: "'
|
||||
+ line
|
||||
+ '"',
|
||||
`Line '${lineNumber}' in 'cli.js' exports something. cli.js is not for exporting. Line was:
|
||||
${line}`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -92,7 +89,9 @@ async function packCliJs(): Promise<void> {
|
||||
const match = line.match(/^(\s*)(const|var) ([a-z0-9_]*) = require\(("[^"]+"|'[^']+')\);$/i);
|
||||
|
||||
if (match !== null) {
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
const importedName = match[3];
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
const moduleName = match[4].substring(1, match[4].length - 1);
|
||||
|
||||
// if it begins with '.' and not ends with json
|
||||
@@ -100,19 +99,23 @@ async function packCliJs(): Promise<void> {
|
||||
|
||||
// is the first internal require
|
||||
if (internalRequire !== null) {
|
||||
return 'const ' + importedName + ' = ' + internalRequire + ';';
|
||||
return `const ${importedName} = ${internalRequire};`;
|
||||
}
|
||||
|
||||
// only the first import needs a require
|
||||
internalRequire = importedName;
|
||||
return 'const ' + importedName + ' = require("./index");';
|
||||
|
||||
return `const ${importedName} = require("./index");`;
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return await writeFilePromisified(path, adjustedContent);
|
||||
return writeFilePromisified(path, `#!/usr/bin/env node
|
||||
|
||||
${adjustedContent}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,11 +130,11 @@ async function getAllTypeDefinitions(): Promise<string[]> {
|
||||
],
|
||||
});
|
||||
|
||||
const promises = fileNames.map((fileName) => {
|
||||
const promises = fileNames.map(async (fileName) => {
|
||||
return readFilePromisified(fileName, 'utf8');
|
||||
});
|
||||
|
||||
return await Promise.all(promises);
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,7 +150,7 @@ async function packTypeDefinitions(): Promise<void> {
|
||||
const typeDefinitions = await getAllTypeDefinitions();
|
||||
|
||||
// pack TypeScript definition files
|
||||
const imports: { [k: string]: string[] } = {};
|
||||
const imports: { [k: string]: string[]; } = {};
|
||||
|
||||
const referenceLines: string[] = [];
|
||||
|
||||
@@ -163,6 +166,7 @@ async function packTypeDefinitions(): Promise<void> {
|
||||
|
||||
if (line.indexOf('/// <reference') === 0) {
|
||||
referenceLines.push(line);
|
||||
|
||||
return '// moved referenced line';
|
||||
}
|
||||
|
||||
@@ -170,13 +174,15 @@ async function packTypeDefinitions(): Promise<void> {
|
||||
const match = line.match(/^import {([^}].*)} from '([^'].*)';$/);
|
||||
|
||||
if (match !== null) {
|
||||
// extract module
|
||||
// tslint:disable-next-line:no-magic-numbers - extract module
|
||||
const module = match[2];
|
||||
|
||||
// extract imported objects
|
||||
const importedObjects = match[1].split(',').map((object) => {
|
||||
return object.trim();
|
||||
});
|
||||
const importedObjects = match[1]
|
||||
.split(',')
|
||||
.map((object) => {
|
||||
return object.trim();
|
||||
});
|
||||
|
||||
// add list of already imported objects for module
|
||||
if (typeof imports[module] === 'undefined') {
|
||||
@@ -195,27 +201,31 @@ async function packTypeDefinitions(): Promise<void> {
|
||||
// replace import line
|
||||
if (objectsToImport.length === 0) {
|
||||
return '// extraneous removed import';
|
||||
} else {
|
||||
return 'import { ' + objectsToImport.join(', ') + ' } from \'' + module + '\';';
|
||||
}
|
||||
|
||||
return `import {${objectsToImport.join(', ')}} from '${module}';`;
|
||||
}
|
||||
|
||||
return line;
|
||||
})
|
||||
// filter lines which contain "local" imports
|
||||
.filter((line) => {
|
||||
return !line.match(/^import .* from '\./);
|
||||
return line.match(/^import .* from '\./) === null;
|
||||
})
|
||||
// concat all lines separated by new lines
|
||||
.join('\n');
|
||||
|
||||
if (allDefinitions.length > 0) {
|
||||
if (referenceLines.length > 0) {
|
||||
allDefinitions = referenceLines.join('\n') + '\n\n' + allDefinitions;
|
||||
allDefinitions = `${referenceLines.join('\n')}
|
||||
|
||||
${allDefinitions}`;
|
||||
}
|
||||
|
||||
// write packed TypeScript definition files
|
||||
return await writeFilePromisified(path, PACK_IDENTIFIER + '\n\n' + allDefinitions);
|
||||
return writeFilePromisified(path, `${PACK_IDENTIFIER}
|
||||
|
||||
${allDefinitions}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,17 +243,21 @@ async function getAllJavaScriptModules(): Promise<JavaScriptModule[]> {
|
||||
|
||||
const promises = fileNames.map(async (fileName) => {
|
||||
const fileContent = await readFilePromisified(fileName, 'utf8');
|
||||
const directory = dirname(fileName).replace(new RegExp('^' + join(cwd(), 'lib')), '');
|
||||
const directory = dirname(fileName)
|
||||
.replace(new RegExp(`^${join(cwd(), 'lib')}`), '');
|
||||
|
||||
return {
|
||||
content: '(function() {\n' + fileContent + '\n})();\n',
|
||||
content: `(function() {
|
||||
${fileContent}
|
||||
})();
|
||||
`,
|
||||
dependencies: getAllInternalDependencies(fileContent),
|
||||
directory: directory,
|
||||
name: basename(fileName, '.js'),
|
||||
};
|
||||
});
|
||||
|
||||
return await Promise.all(promises);
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,13 +285,16 @@ async function packJavaScriptFiles(): Promise<void> {
|
||||
|
||||
// replace lines with internal requires
|
||||
if (match !== null) {
|
||||
// match[6] or match[8] contain the modulePath
|
||||
// tslint:disable-next-line:no-magic-numbers - match[6] or match[8] contain the modulePath
|
||||
if (typeof match[6] === 'undefined') {
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
match[6] = match[8];
|
||||
}
|
||||
|
||||
const whiteSpace = match[1] ? match[1] : '';
|
||||
const whiteSpace = (typeof match[1] === 'string' && match[1].length > 0) ? match[1] : '';
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
const importedName = match[3];
|
||||
// tslint:disable-next-line:no-magic-numbers
|
||||
const modulePath = match[6];
|
||||
|
||||
// leave line unchanged if it is a "global" import
|
||||
@@ -286,22 +303,23 @@ async function packJavaScriptFiles(): Promise<void> {
|
||||
}
|
||||
|
||||
// replace internal requires with `module.exports`
|
||||
if (existsSync(join(cwd(), 'lib', module.directory, modulePath + '.js'))) {
|
||||
return whiteSpace + 'const ' + importedName + ' = module.exports;';
|
||||
if (existsSync(join(cwd(), 'lib', module.directory, `${modulePath}.js`))) {
|
||||
return `${whiteSpace}const ${importedName} = module.exports;`;
|
||||
}
|
||||
|
||||
if (existsSync(join(cwd(), 'src', module.directory, modulePath))) {
|
||||
return whiteSpace + 'const ' + importedName + ' = require(\'../src/' + modulePath + '\');';
|
||||
return `${whiteSpace} const ${importedName} = require(../src/${modulePath});`;
|
||||
}
|
||||
|
||||
Logger.warn('Import ' + importedName + ' could not be found in module.directory ' + modulePath);
|
||||
Logger.warn(`Import ${importedName} could not be found in module.directory ${modulePath}.`);
|
||||
}
|
||||
|
||||
return line;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return '// Module: ' + module.name + '\n' + module.content;
|
||||
return `// Module: ${module.name}
|
||||
${module.content}`;
|
||||
})
|
||||
// concat them separated by new lines
|
||||
.join('\n\n\n\n\n')
|
||||
@@ -332,10 +350,15 @@ async function packJavaScriptFiles(): Promise<void> {
|
||||
|
||||
if (wholeCode.length > 0) {
|
||||
// add meta lines to the file
|
||||
wholeCode = '"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });\n\n' + wholeCode;
|
||||
wholeCode = `"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
|
||||
${wholeCode}`;
|
||||
|
||||
// write packed JavaScript files
|
||||
return await writeFilePromisified(path, PACK_IDENTIFIER + '\n\n' + wholeCode);
|
||||
return writeFilePromisified(path, `${PACK_IDENTIFIER}
|
||||
|
||||
${wholeCode}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,8 +374,9 @@ async function deleteFileIfExistingAndPacked(path: string): Promise<void> {
|
||||
|
||||
// check if packed by this script
|
||||
if (content.indexOf(PACK_IDENTIFIER) === 0) {
|
||||
Logger.log('Found `' + path + '` which is packed by this script. Deleting it...');
|
||||
return await unlinkPromisified(path);
|
||||
Logger.log(`Found '${path}' which is packed by this script. Deleting it...`);
|
||||
|
||||
return unlinkPromisified(path);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
@@ -372,23 +396,26 @@ function getAllInternalDependencies(moduleContent: string): string[] {
|
||||
moduleContent.match(/^\s*(const|var) [a-z0-9_]* = require\("([^"]+)"\)|require\('([^']+)'\);$/gmi);
|
||||
|
||||
if (Array.isArray(requireLines)) {
|
||||
return requireLines.map((requireLine) => {
|
||||
const matches = requireLine.match(/require\("([^"]+)"\)|require\('([^']+)'\);$/i);
|
||||
return requireLines
|
||||
.map((requireLine) => {
|
||||
const matches = requireLine.match(/require\("([^"]+)"\)|require\('([^']+)'\);$/i);
|
||||
|
||||
// previously matched require line does not contain a require?!
|
||||
if (matches === null) {
|
||||
throw new Error();
|
||||
}
|
||||
// previously matched require line does not contain a require?!
|
||||
if (matches === null) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
// return only the moduleName
|
||||
return matches[1];
|
||||
}).filter((moduleName) => {
|
||||
// filter out internal modules beginning with './' and not ending with '.json'
|
||||
return /^[.]{1,2}\/(?!.*\.json$).*$/i.test(moduleName);
|
||||
}).map((internalModuleName) => {
|
||||
// cut './' from the name
|
||||
return internalModuleName.substring(2);
|
||||
});
|
||||
// return only the moduleName
|
||||
return matches[1];
|
||||
})
|
||||
.filter((moduleName) => {
|
||||
// filter out internal modules beginning with './' and not ending with '.json'
|
||||
return /^[.]{1,2}\/(?!.*\.json$).*$/i.test(moduleName);
|
||||
})
|
||||
.map((internalModuleName) => {
|
||||
// cut './' from the name
|
||||
return internalModuleName.substring('./'.length);
|
||||
});
|
||||
}
|
||||
|
||||
return [];
|
||||
@@ -417,11 +444,13 @@ function topologicalSort(modules: JavaScriptModule[]): JavaScriptModule[] {
|
||||
});
|
||||
|
||||
// sort graph and return as an array of sorted modules
|
||||
return topoSort.array(nodes, edges).map((moduleName: string) => {
|
||||
return modules.find((module) => {
|
||||
return module.name === moduleName;
|
||||
return topoSort
|
||||
.array(nodes, edges)
|
||||
.map((moduleName: string) => {
|
||||
return modules.find((module) => {
|
||||
return module.name === moduleName;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user