mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 00:52:55 +00:00
feat: add projectmanagement and pack
This commit is contained in:
128
src/pack/pack-definitions.ts
Normal file
128
src/pack/pack-definitions.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright (C) 2018 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {join} from 'path';
|
||||
import {cwd} from 'process';
|
||||
import {
|
||||
deleteFileIfExistingAndPacked,
|
||||
globPromisified,
|
||||
logger,
|
||||
readFilePromisified,
|
||||
writeFilePromisified,
|
||||
} from '../common';
|
||||
import {PACK_IDENTIFIER} from '../configuration';
|
||||
|
||||
/**
|
||||
* Get a list containing the contents of all type definition files
|
||||
*/
|
||||
async function getAllTypeDefinitions(): Promise<string[]> {
|
||||
const fileNames = await globPromisified(join(cwd(), '*(lib|src)', '**', '*.d.ts'), {
|
||||
ignore: [
|
||||
join(cwd(), 'lib', 'doc', '**', '*.d.ts'),
|
||||
join(cwd(), 'lib', 'test', '**', '*.d.ts'),
|
||||
join(cwd(), 'lib', 'cli.d.ts'),
|
||||
],
|
||||
});
|
||||
|
||||
const promises = fileNames.map((fileName) => {
|
||||
return readFilePromisified(fileName, 'utf8');
|
||||
});
|
||||
|
||||
return await Promise.all(promises);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pack a list of type definitions into one file
|
||||
*/
|
||||
export async function packTypeDefinitions(): Promise<void> {
|
||||
logger.info('Packing TypeScript definition files...');
|
||||
|
||||
const path = join(cwd(), 'lib', 'index.d.ts');
|
||||
|
||||
await deleteFileIfExistingAndPacked(path);
|
||||
|
||||
const typeDefinitions = await getAllTypeDefinitions();
|
||||
|
||||
// pack TypeScript definition files
|
||||
const imports: { [k: string]: string[] } = {};
|
||||
|
||||
const referenceLines: string[] = [];
|
||||
|
||||
let allDefinitions = typeDefinitions
|
||||
// concat them separated by new lines
|
||||
.join('\n\n\n\n\n')
|
||||
// split all lines
|
||||
.split('\n')
|
||||
.map((line) => {
|
||||
if (line.indexOf('export =') !== -1) {
|
||||
throw new Error('`export =` is not allowed by pack. Use named imports instead.');
|
||||
}
|
||||
|
||||
if (line.indexOf('/// <reference') === 0) {
|
||||
referenceLines.push(line);
|
||||
return '// moved referenced line';
|
||||
}
|
||||
|
||||
// match import lines
|
||||
const match = line.match(/^import {([^}].*)} from '([^'].*)';$/);
|
||||
|
||||
if (match !== null) {
|
||||
// extract module
|
||||
const module = match[2];
|
||||
|
||||
// extract imported objects
|
||||
const importedObjects = match[1].split(',').map((object) => {
|
||||
return object.trim();
|
||||
});
|
||||
|
||||
// add list of already imported objects for module
|
||||
if (typeof imports[module] === 'undefined') {
|
||||
imports[module] = [];
|
||||
}
|
||||
|
||||
// count already imported objects and objects to import now
|
||||
const objectsToImport: string[] = [];
|
||||
importedObjects.forEach((object) => {
|
||||
if (imports[module].indexOf(object) === -1) {
|
||||
imports[module].push(object);
|
||||
objectsToImport.push(object);
|
||||
}
|
||||
});
|
||||
|
||||
// replace import line
|
||||
if (objectsToImport.length === 0) {
|
||||
return '// extraneous removed import';
|
||||
} else {
|
||||
return 'import { ' + objectsToImport.join(', ') + ' } from \'' + module + '\';';
|
||||
}
|
||||
}
|
||||
|
||||
return line;
|
||||
})
|
||||
// filter lines which contain "local" imports
|
||||
.filter((line) => {
|
||||
return !line.match(/^import .* from '\./);
|
||||
})
|
||||
// concat all lines separated by new lines
|
||||
.join('\n');
|
||||
|
||||
if (allDefinitions.length > 0) {
|
||||
if (referenceLines.length > 0) {
|
||||
allDefinitions = referenceLines.join('\n') + '\n\n' + allDefinitions;
|
||||
}
|
||||
|
||||
// write packed TypeScript definition files
|
||||
return await writeFilePromisified(path, PACK_IDENTIFIER + '\n\n' + allDefinitions);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user