mirror of
https://github.com/strapi/strapi.git
synced 2025-09-20 05:52:08 +00:00
Add typescript utils to the utils (commands, tools, compilers)
This commit is contained in:
parent
c33742c72b
commit
ad1db987b5
9
packages/core/strapi/lib/utils/import-default.js
Normal file
9
packages/core/strapi/lib/utils/import-default.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const __importDefault =
|
||||||
|
(this && this.__importDefault) ||
|
||||||
|
function(mod) {
|
||||||
|
return mod && mod.__esModule ? mod : { default: mod };
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = __importDefault;
|
@ -3,9 +3,11 @@
|
|||||||
const openBrowser = require('./open-browser');
|
const openBrowser = require('./open-browser');
|
||||||
const isInitialized = require('./is-initialized');
|
const isInitialized = require('./is-initialized');
|
||||||
const getDirs = require('./get-dirs');
|
const getDirs = require('./get-dirs');
|
||||||
|
const ts = require('./typescript');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isInitialized,
|
isInitialized,
|
||||||
openBrowser,
|
openBrowser,
|
||||||
getDirs,
|
getDirs,
|
||||||
|
ts,
|
||||||
};
|
};
|
||||||
|
18
packages/core/strapi/lib/utils/typescript/commands/build.js
Normal file
18
packages/core/strapi/lib/utils/typescript/commands/build.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const compilers = require('../compilers');
|
||||||
|
const getConfigPath = require('../get-config-path');
|
||||||
|
const copyResources = require('../copy-resources');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} dir
|
||||||
|
*/
|
||||||
|
module.exports = async dir => {
|
||||||
|
console.log('Compiling TypeScript files...');
|
||||||
|
|
||||||
|
const configPath = getConfigPath(dir);
|
||||||
|
|
||||||
|
compilers.basic.run(configPath);
|
||||||
|
|
||||||
|
await copyResources(dir);
|
||||||
|
};
|
@ -0,0 +1,18 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const compilers = require('../compilers');
|
||||||
|
const getConfigPath = require('../get-config-path');
|
||||||
|
const copyResources = require('../copy-resources');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} dir
|
||||||
|
*/
|
||||||
|
module.exports = async dir => {
|
||||||
|
console.log('Compiling TypeScript files and watching for files change...');
|
||||||
|
|
||||||
|
const configPath = getConfigPath(dir);
|
||||||
|
|
||||||
|
compilers.watch.run(configPath);
|
||||||
|
|
||||||
|
await copyResources(dir);
|
||||||
|
};
|
@ -0,0 +1,9 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const build = require('./build');
|
||||||
|
const develop = require('./develop');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
build,
|
||||||
|
develop,
|
||||||
|
};
|
38
packages/core/strapi/lib/utils/typescript/compilers/basic.js
Normal file
38
packages/core/strapi/lib/utils/typescript/compilers/basic.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
const reportDiagnostics = require('../report-diagnostics');
|
||||||
|
const resolveConfigOptions = require('../resolve-config-options');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* Default TS -> JS Compilation for Strapi
|
||||||
|
* @param {string} tsConfigPath
|
||||||
|
*/
|
||||||
|
run(tsConfigPath) {
|
||||||
|
// Parse the tsconfig.json file & resolve the configuration options
|
||||||
|
const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);
|
||||||
|
|
||||||
|
const program = ts.createProgram({
|
||||||
|
rootNames: fileNames,
|
||||||
|
projectReferences,
|
||||||
|
options,
|
||||||
|
});
|
||||||
|
|
||||||
|
const emitResults = program.emit();
|
||||||
|
|
||||||
|
const diagnostics = ts.sortAndDeduplicateDiagnostics(
|
||||||
|
ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (diagnostics.length > 0) {
|
||||||
|
reportDiagnostics(diagnostics);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the compilation failed, exit early
|
||||||
|
if (emitResults.emitSkipped) {
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
@ -0,0 +1,9 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const basic = require('./basic');
|
||||||
|
const watch = require('./watch');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
basic,
|
||||||
|
watch,
|
||||||
|
};
|
37
packages/core/strapi/lib/utils/typescript/compilers/watch.js
Normal file
37
packages/core/strapi/lib/utils/typescript/compilers/watch.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
const reportDiagnostics = require('../report-diagnostics');
|
||||||
|
const formatHost = require('../format-host');
|
||||||
|
const resolveConfigOptions = require('../resolve-config-options');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints a diagnostic every time the watch status changes.
|
||||||
|
* This is mainly for messages like "Starting compilation" or "Compilation completed".
|
||||||
|
*/
|
||||||
|
const reportWatchStatusChanged = diagnostic => {
|
||||||
|
console.info(ts.formatDiagnostic(diagnostic, formatHost));
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
run(configPath) {
|
||||||
|
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
|
||||||
|
|
||||||
|
const { fileNames, options, projectReferences, watchOptions } = resolveConfigOptions(
|
||||||
|
configPath
|
||||||
|
);
|
||||||
|
const host = ts.createWatchCompilerHost(
|
||||||
|
fileNames,
|
||||||
|
options,
|
||||||
|
ts.sys,
|
||||||
|
createProgram,
|
||||||
|
reportDiagnostics,
|
||||||
|
reportWatchStatusChanged,
|
||||||
|
projectReferences,
|
||||||
|
watchOptions
|
||||||
|
);
|
||||||
|
|
||||||
|
ts.createWatchProgram(host);
|
||||||
|
},
|
||||||
|
};
|
12
packages/core/strapi/lib/utils/typescript/copy-resources.js
Normal file
12
packages/core/strapi/lib/utils/typescript/copy-resources.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const path = require('path');
|
||||||
|
const fse = require('fs-extra');
|
||||||
|
|
||||||
|
const DEFAULT_RESOURCES_PATHS = ['public', 'favicon.ico', 'package.json'];
|
||||||
|
|
||||||
|
module.exports = async (dir, resources = DEFAULT_RESOURCES_PATHS) => {
|
||||||
|
await Promise.all(
|
||||||
|
resources.map(resourceName => fse.copy(resourceName, path.join(dir, 'dist', resourceName)))
|
||||||
|
);
|
||||||
|
};
|
15
packages/core/strapi/lib/utils/typescript/format-host.js
Normal file
15
packages/core/strapi/lib/utils/typescript/format-host.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const ts = require('typescript');
|
||||||
|
const { identity } = require('lodash/fp');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {ts.FormatDiagnosticsHost}
|
||||||
|
*/
|
||||||
|
const formatHost = {
|
||||||
|
getCanonicalFileName: identity,
|
||||||
|
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
||||||
|
getNewLine: () => ts.sys.newLine,
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = formatHost;
|
@ -0,0 +1,9 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
const DEFAULT_FILE_NAME = 'tsconfig.json';
|
||||||
|
|
||||||
|
module.exports = (dir, filename = DEFAULT_FILE_NAME) => {
|
||||||
|
return ts.findConfigFile(dir, ts.sys.fileExists, filename);
|
||||||
|
};
|
23
packages/core/strapi/lib/utils/typescript/index.js
Normal file
23
packages/core/strapi/lib/utils/typescript/index.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const isTypeScriptProject = require('./is-typescript-project');
|
||||||
|
const getConfigPath = require('./get-config-path');
|
||||||
|
const reportDiagnostics = require('./report-diagnostics');
|
||||||
|
const resolveConfigOptions = require('./resolve-config-options');
|
||||||
|
const copyResources = require('./copy-resources');
|
||||||
|
const formatHost = require('./format-host');
|
||||||
|
|
||||||
|
const compilers = require('./compilers');
|
||||||
|
const commands = require('./commands');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
isTypeScriptProject,
|
||||||
|
getConfigPath,
|
||||||
|
reportDiagnostics,
|
||||||
|
resolveConfigOptions,
|
||||||
|
copyResources,
|
||||||
|
formatHost,
|
||||||
|
|
||||||
|
compilers,
|
||||||
|
commands,
|
||||||
|
};
|
@ -0,0 +1,11 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const fse = require('fs-extra');
|
||||||
|
|
||||||
|
const getConfigPath = require('./get-config-path');
|
||||||
|
|
||||||
|
module.exports = (dir, filename = undefined) => {
|
||||||
|
const filePath = getConfigPath(dir, filename);
|
||||||
|
|
||||||
|
return fse.pathExists(filePath);
|
||||||
|
};
|
@ -0,0 +1,19 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
const formatHost = require('./format-host');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report one or several diagnostic to the console
|
||||||
|
* @param {ts.Diagnostic[] | ts.Diagnostic} diagnostics
|
||||||
|
*/
|
||||||
|
module.exports = diagnostics => {
|
||||||
|
const formattedDiagnostics = ts.formatDiagnosticsWithColorAndContext(
|
||||||
|
Array.isArray(diagnostics) ? diagnostics : [diagnostics],
|
||||||
|
formatHost
|
||||||
|
);
|
||||||
|
|
||||||
|
console.error(formattedDiagnostics);
|
||||||
|
console.info(`Found ${diagnostics.length} error(s).${ts.sys.newLine}`);
|
||||||
|
};
|
@ -0,0 +1,23 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
const logDiagnostics = require('./report-diagnostics');
|
||||||
|
|
||||||
|
module.exports = configPath => {
|
||||||
|
// Parse the tsconfig.json file and resolve every file name & compiler options
|
||||||
|
const { errors, ...configOptions } = ts.getParsedCommandLineOfConfigFile(
|
||||||
|
configPath,
|
||||||
|
undefined,
|
||||||
|
ts.sys
|
||||||
|
);
|
||||||
|
|
||||||
|
// If there are errors in the tsconfig.json
|
||||||
|
// file, report them and exit early
|
||||||
|
if (errors.length > 0) {
|
||||||
|
logDiagnostics(errors);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return configOptions;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user