@strapi/typescript-utils 4.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015-present Strapi Solutions SAS
2
+
3
+ Portions of the Strapi software are licensed as follows:
4
+
5
+ * All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined in "ee/LICENSE".
6
+
7
+ * All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.
8
+
9
+ MIT Expat License
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/lib/compile.js ADDED
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const compilers = require('./compilers');
4
+ const getConfigPath = require('./utils/get-config-path');
5
+ const copyResources = require('./utils/copy-resources');
6
+
7
+ module.exports = async (srcDir, { watch = false } = {}) => {
8
+ // TODO: Use the Strapi debug logger instead or don't log at all
9
+ console.log(`Starting the compilation for TypeScript files in ${srcDir}`);
10
+
11
+ const compiler = watch ? compilers.watch : compilers.basic;
12
+ const configPath = getConfigPath(srcDir);
13
+
14
+ compiler.run(configPath);
15
+
16
+ await copyResources(srcDir);
17
+ };
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+
5
+ const reportDiagnostics = require('../utils/report-diagnostics');
6
+ const resolveConfigOptions = require('../utils/resolve-config-options');
7
+
8
+ module.exports = {
9
+ /**
10
+ * Default TS -> JS Compilation for Strapi
11
+ * @param {string} tsConfigPath
12
+ */
13
+ run(tsConfigPath) {
14
+ // Parse the tsconfig.json file & resolve the configuration options
15
+ const { fileNames, options, projectReferences } = resolveConfigOptions(tsConfigPath);
16
+
17
+ const program = ts.createProgram({
18
+ rootNames: fileNames,
19
+ projectReferences,
20
+ options,
21
+ });
22
+
23
+ const emitResults = program.emit();
24
+
25
+ const diagnostics = ts.sortAndDeduplicateDiagnostics(
26
+ ts.getPreEmitDiagnostics(program).concat(emitResults.diagnostics)
27
+ );
28
+
29
+ if (diagnostics.length > 0) {
30
+ reportDiagnostics(diagnostics);
31
+ }
32
+
33
+ // If the compilation failed, exit early
34
+ if (emitResults.emitSkipped) {
35
+ process.exit(1);
36
+ }
37
+ },
38
+ };
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const basic = require('./basic');
4
+ const watch = require('./watch');
5
+
6
+ module.exports = {
7
+ basic,
8
+ watch,
9
+ };
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+
5
+ const reportDiagnostics = require('../utils/report-diagnostics');
6
+ const formatHost = require('../utils/format-host');
7
+ const resolveConfigOptions = require('../utils/resolve-config-options');
8
+
9
+ /**
10
+ * Prints a diagnostic every time the watch status changes.
11
+ * This is mainly for messages like "Starting compilation" or "Compilation completed".
12
+ */
13
+ const reportWatchStatusChanged = diagnostic => {
14
+ console.info(ts.formatDiagnostic(diagnostic, formatHost));
15
+ };
16
+
17
+ module.exports = {
18
+ run(configPath) {
19
+ const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
20
+
21
+ const { fileNames, options, projectReferences, watchOptions } = resolveConfigOptions(
22
+ configPath
23
+ );
24
+ const host = ts.createWatchCompilerHost(
25
+ fileNames,
26
+ options,
27
+ ts.sys,
28
+ createProgram,
29
+ reportDiagnostics,
30
+ reportWatchStatusChanged,
31
+ projectReferences,
32
+ watchOptions
33
+ );
34
+
35
+ ts.createWatchProgram(host);
36
+ },
37
+ };
package/lib/index.js ADDED
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const compile = require('./compile');
4
+ const compilers = require('./compilers');
5
+ const utils = require('./utils');
6
+
7
+ module.exports = {
8
+ compile,
9
+ compilers,
10
+
11
+ ...utils,
12
+ };
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fse = require('fs-extra');
5
+
6
+ const DEFAULT_RESOURCES_PATHS = ['public', 'favicon.ico', 'package.json'];
7
+
8
+ module.exports = async (dir, resources = DEFAULT_RESOURCES_PATHS) => {
9
+ await Promise.all(
10
+ resources.map(resourceName => fse.copy(resourceName, path.join(dir, 'dist', resourceName)))
11
+ );
12
+ };
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+ const { identity } = require('lodash/fp');
5
+
6
+ /**
7
+ * @type {ts.FormatDiagnosticsHost}
8
+ */
9
+ const formatHost = {
10
+ getCanonicalFileName: identity,
11
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
12
+ getNewLine: () => ts.sys.newLine,
13
+ };
14
+
15
+ module.exports = formatHost;
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+
5
+ const DEFAULT_FILE_NAME = 'tsconfig-server.json';
6
+
7
+ module.exports = (dir, filename = DEFAULT_FILE_NAME) => {
8
+ return ts.findConfigFile(dir, ts.sys.fileExists, filename);
9
+ };
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ const isTypeScriptProject = require('./is-typescript-project');
4
+ const isTypeScriptProjectSync = require('./is-typescript-project-sync');
5
+ const getConfigPath = require('./get-config-path');
6
+ const reportDiagnostics = require('./report-diagnostics');
7
+ const resolveConfigOptions = require('./resolve-config-options');
8
+ const copyResources = require('./copy-resources');
9
+ const formatHost = require('./format-host');
10
+
11
+ module.exports = {
12
+ isTypeScriptProject,
13
+ isTypeScriptProjectSync,
14
+ getConfigPath,
15
+ reportDiagnostics,
16
+ resolveConfigOptions,
17
+ copyResources,
18
+ formatHost,
19
+ };
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const fse = require('fs-extra');
4
+
5
+ const getConfigPath = require('./get-config-path');
6
+
7
+ /**
8
+ * Checks if `dir` is a TypeScript directory (whether there is a tsconfig file or not)
9
+ * @param {string} dir
10
+ * @param {string | undefined} filename
11
+ * @returns {boolean}
12
+ */
13
+ module.exports = (dir, filename = undefined) => {
14
+ const filePath = getConfigPath(dir, filename);
15
+
16
+ return fse.pathExistsSync(filePath);
17
+ };
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ const fse = require('fs-extra');
4
+
5
+ const getConfigPath = require('./get-config-path');
6
+
7
+ /**
8
+ * Checks if `dir` is a TypeScript directory (whether there is a tsconfig file or not)
9
+ * @param {string} dir
10
+ * @param {string | undefined} filename
11
+ * @returns {Promise<boolean>}
12
+ */
13
+ module.exports = (dir, filename = undefined) => {
14
+ const filePath = getConfigPath(dir, filename);
15
+
16
+ return fse.pathExists(filePath);
17
+ };
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+
5
+ const formatHost = require('./format-host');
6
+
7
+ /**
8
+ * Report one or several diagnostic to the console
9
+ * @param {ts.Diagnostic[] | ts.Diagnostic} diagnostics
10
+ */
11
+ module.exports = diagnostics => {
12
+ const formattedDiagnostics = ts.formatDiagnosticsWithColorAndContext(
13
+ Array.isArray(diagnostics) ? diagnostics : [diagnostics],
14
+ formatHost
15
+ );
16
+
17
+ console.error(formattedDiagnostics);
18
+ console.info(`Found ${diagnostics.length} error(s).${ts.sys.newLine}`);
19
+ };
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ const ts = require('typescript');
4
+
5
+ const logDiagnostics = require('./report-diagnostics');
6
+
7
+ module.exports = configPath => {
8
+ // Parse the tsconfig.json file and resolve every file name & compiler options
9
+ const { errors, ...configOptions } = ts.getParsedCommandLineOfConfigFile(
10
+ configPath,
11
+ undefined,
12
+ ts.sys
13
+ );
14
+
15
+ // If there are errors in the tsconfig.json
16
+ // file, report them and exit early
17
+ if (errors.length > 0) {
18
+ logDiagnostics(errors);
19
+ process.exit(1);
20
+ }
21
+
22
+ return configOptions;
23
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@strapi/typescript-utils",
3
+ "version": "4.1.3",
4
+ "description": "Typescript support for Strapi",
5
+ "keywords": [
6
+ "strapi",
7
+ "generators"
8
+ ],
9
+ "license": "SEE LICENSE IN LICENSE",
10
+ "author": {
11
+ "name": "Strapi Solutions SAS",
12
+ "email": "hi@strapi.io",
13
+ "url": "https://strapi.io"
14
+ },
15
+ "maintainers": [
16
+ {
17
+ "name": "Strapi Solutions SAS",
18
+ "email": "hi@strapi.io",
19
+ "url": "https://strapi.io"
20
+ }
21
+ ],
22
+ "main": "./lib/index.js",
23
+ "directories": {
24
+ "lib": "./lib"
25
+ },
26
+ "dependencies": {
27
+ "lodash": "4.17.21",
28
+ "fs-extra": "10.0.1",
29
+ "typescript": "4.6.2"
30
+ },
31
+ "engines": {
32
+ "node": ">=12.22.0 <=16.x.x",
33
+ "npm": ">=6.0.0"
34
+ }
35
+ }