@gravity-ui/app-builder 0.0.1
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/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +46 -0
- package/dist/commands/build/build-lib.d.ts +2 -0
- package/dist/commands/build/build-lib.js +39 -0
- package/dist/commands/build/build-service/client.d.ts +2 -0
- package/dist/commands/build/build-service/client.js +8 -0
- package/dist/commands/build/build-service/index.d.ts +2 -0
- package/dist/commands/build/build-service/index.js +46 -0
- package/dist/commands/build/build-service/server.d.ts +2 -0
- package/dist/commands/build/build-service/server.js +44 -0
- package/dist/commands/build/index.d.ts +2 -0
- package/dist/commands/build/index.js +33 -0
- package/dist/commands/dev/client.d.ts +3 -0
- package/dist/commands/dev/client.js +98 -0
- package/dist/commands/dev/index.d.ts +2 -0
- package/dist/commands/dev/index.js +101 -0
- package/dist/commands/dev/server.d.ts +3 -0
- package/dist/commands/dev/server.js +38 -0
- package/dist/common/babel/index.d.ts +15 -0
- package/dist/common/babel/index.js +17 -0
- package/dist/common/babel/ui-preset.d.ts +1 -0
- package/dist/common/babel/ui-preset.js +71 -0
- package/dist/common/child-process/controllable-script.d.ts +19 -0
- package/dist/common/child-process/controllable-script.js +125 -0
- package/dist/common/child-process/utils.d.ts +1 -0
- package/dist/common/child-process/utils.js +68 -0
- package/dist/common/command.d.ts +4 -0
- package/dist/common/command.js +2 -0
- package/dist/common/config.d.ts +5 -0
- package/dist/common/config.js +155 -0
- package/dist/common/env.d.ts +1 -0
- package/dist/common/env.js +5 -0
- package/dist/common/library/index.d.ts +2 -0
- package/dist/common/library/index.js +302 -0
- package/dist/common/links/link.d.ts +1 -0
- package/dist/common/links/link.js +109 -0
- package/dist/common/links/unlink.d.ts +3 -0
- package/dist/common/links/unlink.js +72 -0
- package/dist/common/logger/colors.d.ts +12 -0
- package/dist/common/logger/colors.js +8 -0
- package/dist/common/logger/index.d.ts +47 -0
- package/dist/common/logger/index.js +120 -0
- package/dist/common/logger/pretty-time.d.ts +2 -0
- package/dist/common/logger/pretty-time.js +51 -0
- package/dist/common/models/index.d.ts +184 -0
- package/dist/common/models/index.js +11 -0
- package/dist/common/package.d.ts +19 -0
- package/dist/common/package.js +42 -0
- package/dist/common/paths.d.ts +20 -0
- package/dist/common/paths.js +28 -0
- package/dist/common/s3-upload/compress.d.ts +2 -0
- package/dist/common/s3-upload/compress.js +23 -0
- package/dist/common/s3-upload/index.d.ts +3 -0
- package/dist/common/s3-upload/index.js +7 -0
- package/dist/common/s3-upload/s3-client.d.ts +20 -0
- package/dist/common/s3-upload/s3-client.js +111 -0
- package/dist/common/s3-upload/upload.d.ts +14 -0
- package/dist/common/s3-upload/upload.js +117 -0
- package/dist/common/s3-upload/webpack-plugin.d.ts +18 -0
- package/dist/common/s3-upload/webpack-plugin.js +53 -0
- package/dist/common/tempData.d.ts +13 -0
- package/dist/common/tempData.js +43 -0
- package/dist/common/typescript/compile.d.ts +5 -0
- package/dist/common/typescript/compile.js +72 -0
- package/dist/common/typescript/diagnostic.d.ts +2 -0
- package/dist/common/typescript/diagnostic.js +46 -0
- package/dist/common/typescript/transformers.d.ts +2 -0
- package/dist/common/typescript/transformers.js +114 -0
- package/dist/common/typescript/utils.d.ts +10 -0
- package/dist/common/typescript/utils.js +53 -0
- package/dist/common/typescript/watch.d.ts +7 -0
- package/dist/common/typescript/watch.js +60 -0
- package/dist/common/utils.d.ts +2 -0
- package/dist/common/utils.js +19 -0
- package/dist/common/webpack/compile.d.ts +2 -0
- package/dist/common/webpack/compile.js +31 -0
- package/dist/common/webpack/config.d.ts +23 -0
- package/dist/common/webpack/config.js +662 -0
- package/dist/common/webpack/progress-plugin.d.ts +11 -0
- package/dist/common/webpack/progress-plugin.js +64 -0
- package/dist/common/webpack/public-path.d.ts +1 -0
- package/dist/common/webpack/public-path.js +2 -0
- package/dist/common/webpack/storybook.d.ts +13 -0
- package/dist/common/webpack/storybook.js +86 -0
- package/dist/common/webpack/utils.d.ts +7 -0
- package/dist/common/webpack/utils.js +71 -0
- package/dist/create-cli.d.ts +54 -0
- package/dist/create-cli.js +221 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/package.json +154 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.uploadFiles = void 0;
|
|
30
|
+
const path = __importStar(require("path"));
|
|
31
|
+
const p_queue_1 = __importDefault(require("p-queue"));
|
|
32
|
+
const s3_client_js_1 = require("./s3-client.js");
|
|
33
|
+
const compress_js_1 = require("./compress.js");
|
|
34
|
+
function uploadFiles(files, config) {
|
|
35
|
+
var _a;
|
|
36
|
+
const s3Client = (0, s3_client_js_1.getS3Client)(config.s3);
|
|
37
|
+
const queue = new p_queue_1.default({
|
|
38
|
+
concurrency: (_a = config.concurrency) !== null && _a !== void 0 ? _a : 512,
|
|
39
|
+
});
|
|
40
|
+
const processFile = fileProcessor(config.options);
|
|
41
|
+
return Promise.all(files.flatMap((filePath) => {
|
|
42
|
+
const relativeFilePath = path.isAbsolute(filePath)
|
|
43
|
+
? path.relative(config.options.sourcePath, filePath)
|
|
44
|
+
: filePath;
|
|
45
|
+
return processFile(relativeFilePath);
|
|
46
|
+
}));
|
|
47
|
+
function doesExist(bucket, key) {
|
|
48
|
+
return queue
|
|
49
|
+
.add(() => s3Client.headObject(bucket, key))
|
|
50
|
+
.then(() => true)
|
|
51
|
+
.catch(() => false);
|
|
52
|
+
}
|
|
53
|
+
function uploadFile(bucket, sourceFilePath, targetFilePath, options) {
|
|
54
|
+
return queue.add(() => s3Client.uploadFile(bucket, sourceFilePath, targetFilePath, options));
|
|
55
|
+
}
|
|
56
|
+
function fileUploader(options) {
|
|
57
|
+
return async (relativeFilePath) => {
|
|
58
|
+
const sourceFilePath = path.join(options.sourcePath, relativeFilePath);
|
|
59
|
+
const targetFilePath = path.join(options.targetPath || '', relativeFilePath);
|
|
60
|
+
console.info(`Uploading file ${relativeFilePath} ...`);
|
|
61
|
+
const exists = await doesExist(options.bucket, targetFilePath);
|
|
62
|
+
if (exists) {
|
|
63
|
+
switch (options.existsBehavior) {
|
|
64
|
+
case 'overwrite': {
|
|
65
|
+
console.info(`File ${targetFilePath} will be overwritten.`);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case 'throw': {
|
|
69
|
+
throw new Error(`File ${targetFilePath} already exists in ${options.bucket}`);
|
|
70
|
+
}
|
|
71
|
+
default: {
|
|
72
|
+
console.info(`Nothing todo with '${relativeFilePath}'`);
|
|
73
|
+
return Promise.resolve(relativeFilePath);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return uploadFile(options.bucket, sourceFilePath, targetFilePath)
|
|
78
|
+
.then(() => {
|
|
79
|
+
console.info(` ${relativeFilePath} => ${targetFilePath}`);
|
|
80
|
+
return relativeFilePath;
|
|
81
|
+
})
|
|
82
|
+
.catch((error) => {
|
|
83
|
+
console.error(`Failed to upload file ${relativeFilePath}`);
|
|
84
|
+
if (error instanceof Error) {
|
|
85
|
+
console.error(`msg: ${error.message}`);
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function compress(sourcePath) {
|
|
92
|
+
return [queue.add(() => (0, compress_js_1.gzip)(sourcePath)), queue.add(() => (0, compress_js_1.brotli)(sourcePath))];
|
|
93
|
+
}
|
|
94
|
+
function fileProcessor(options) {
|
|
95
|
+
return (relativeFilePath) => {
|
|
96
|
+
const upload = fileUploader(options);
|
|
97
|
+
const filesPromises = [upload(relativeFilePath)];
|
|
98
|
+
if (config.compress && shouldCompress(relativeFilePath)) {
|
|
99
|
+
const sourcePath = path.join(options.sourcePath, relativeFilePath);
|
|
100
|
+
filesPromises.push(...compress(sourcePath).map((promise) => {
|
|
101
|
+
return promise.then((compressedFile) => {
|
|
102
|
+
const relativeCompressedFilePath = path.relative(options.sourcePath, compressedFile);
|
|
103
|
+
return upload(relativeCompressedFilePath);
|
|
104
|
+
});
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
return filesPromises;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
exports.uploadFiles = uploadFiles;
|
|
112
|
+
const NOT_COMPRESS = ['png', 'zip', 'gz', 'br'];
|
|
113
|
+
function shouldCompress(filePath) {
|
|
114
|
+
const fileName = path.basename(filePath);
|
|
115
|
+
const extension = fileName.split('.').pop();
|
|
116
|
+
return !NOT_COMPRESS.includes(extension);
|
|
117
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { UploadOptions } from './upload.js';
|
|
2
|
+
import type { Compiler } from 'webpack';
|
|
3
|
+
import type { S3ClientOptions } from './s3-client.js';
|
|
4
|
+
interface S3UploadPluginOptions {
|
|
5
|
+
include?: Rule | Rule[];
|
|
6
|
+
exclude?: Rule | Rule[];
|
|
7
|
+
compress?: boolean;
|
|
8
|
+
s3ClientOptions: S3ClientOptions;
|
|
9
|
+
s3UploadOptions: Pick<UploadOptions, 'bucket' | 'targetPath' | 'existsBehavior'>;
|
|
10
|
+
}
|
|
11
|
+
export declare class S3UploadPlugin {
|
|
12
|
+
private options;
|
|
13
|
+
constructor(options: S3UploadPluginOptions);
|
|
14
|
+
apply(compiler: Compiler): void;
|
|
15
|
+
private isIncludeAndNotExclude;
|
|
16
|
+
}
|
|
17
|
+
type Rule = RegExp | string | ((s: string) => boolean);
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.S3UploadPlugin = void 0;
|
|
4
|
+
const webpack_1 = require("webpack");
|
|
5
|
+
const upload_js_1 = require("./upload.js");
|
|
6
|
+
class S3UploadPlugin {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
}
|
|
10
|
+
apply(compiler) {
|
|
11
|
+
compiler.hooks.done.tapPromise('S3UploadPlugin', async ({ compilation }) => {
|
|
12
|
+
var _a;
|
|
13
|
+
const fileNames = Object.keys(compilation.assets).filter((name) => {
|
|
14
|
+
const fullPath = compilation.outputOptions.path + '/' + name;
|
|
15
|
+
return this.isIncludeAndNotExclude(fullPath);
|
|
16
|
+
});
|
|
17
|
+
try {
|
|
18
|
+
await (0, upload_js_1.uploadFiles)(fileNames, {
|
|
19
|
+
s3: this.options.s3ClientOptions,
|
|
20
|
+
compress: this.options.compress,
|
|
21
|
+
options: Object.assign(Object.assign({}, this.options.s3UploadOptions), { sourcePath: (_a = compilation.outputOptions.path) !== null && _a !== void 0 ? _a : '' }),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
const error = new webpack_1.WebpackError(`${e instanceof Error ? e.message : e}`);
|
|
26
|
+
compilation.errors.push(error);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
isIncludeAndNotExclude(fileName) {
|
|
31
|
+
const { include, exclude } = this.options;
|
|
32
|
+
const isInclude = include ? testRule(include, fileName) : true;
|
|
33
|
+
const isExclude = exclude ? testRule(exclude, fileName) : false;
|
|
34
|
+
return isInclude && !isExclude;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.S3UploadPlugin = S3UploadPlugin;
|
|
38
|
+
function testRule(rule, source) {
|
|
39
|
+
if (rule instanceof RegExp) {
|
|
40
|
+
return rule.test(source);
|
|
41
|
+
}
|
|
42
|
+
if (typeof rule === 'function') {
|
|
43
|
+
return Boolean(rule(source));
|
|
44
|
+
}
|
|
45
|
+
if (typeof rule === 'string') {
|
|
46
|
+
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
47
|
+
return new RegExp(rule).test(source);
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(rule)) {
|
|
50
|
+
return rule.some((condition) => testRule(condition, source));
|
|
51
|
+
}
|
|
52
|
+
throw new Error('Unknown type of rule');
|
|
53
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Entities, LinkedPackage } from './models';
|
|
2
|
+
interface TempData {
|
|
3
|
+
linkedPackages?: Entities<LinkedPackage>;
|
|
4
|
+
}
|
|
5
|
+
declare class TempDataUtils {
|
|
6
|
+
private state;
|
|
7
|
+
constructor();
|
|
8
|
+
saveSettings(newSettings?: Partial<TempData>): boolean;
|
|
9
|
+
getSettings(): Partial<TempData>;
|
|
10
|
+
clearSetting(settingName: keyof TempData): void;
|
|
11
|
+
}
|
|
12
|
+
declare const _default: TempDataUtils;
|
|
13
|
+
export default _default;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const path_1 = __importDefault(require("path"));
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const paths_1 = __importDefault(require("./paths"));
|
|
9
|
+
const logger_1 = __importDefault(require("./logger"));
|
|
10
|
+
const tempDataPath = path_1.default.resolve(paths_1.default.appNodeModules, '.app-builder.temp.json');
|
|
11
|
+
class TempDataUtils {
|
|
12
|
+
constructor() {
|
|
13
|
+
try {
|
|
14
|
+
const settingsJson = fs_1.default.readFileSync(tempDataPath, 'utf-8');
|
|
15
|
+
this.state = JSON.parse(settingsJson);
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
this.state = {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
saveSettings(newSettings = {}) {
|
|
22
|
+
this.state = Object.assign(Object.assign({}, this.state), newSettings);
|
|
23
|
+
if (fs_1.default.existsSync(tempDataPath)) {
|
|
24
|
+
fs_1.default.unlinkSync(tempDataPath);
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
fs_1.default.writeFileSync(tempDataPath, JSON.stringify(this.state), 'utf-8');
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
logger_1.default.logError(e);
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
getSettings() {
|
|
36
|
+
return this.state;
|
|
37
|
+
}
|
|
38
|
+
clearSetting(settingName) {
|
|
39
|
+
delete this.state[settingName];
|
|
40
|
+
this.saveSettings();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.default = new TempDataUtils();
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compile = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const transformers_1 = require("./transformers");
|
|
6
|
+
const pretty_time_1 = require("../logger/pretty-time");
|
|
7
|
+
const diagnostic_1 = require("./diagnostic");
|
|
8
|
+
function compile(ts, projectPath, { logger }) {
|
|
9
|
+
const start = process.hrtime.bigint();
|
|
10
|
+
logger.message('Start compilation');
|
|
11
|
+
logger.message(`Typescript v${ts.version}`);
|
|
12
|
+
logger.verbose(`Searching for the tsconfig.json in ${projectPath}`);
|
|
13
|
+
const configPath = (0, utils_1.getProjectConfig)(ts, projectPath);
|
|
14
|
+
const formatHost = {
|
|
15
|
+
getCanonicalFileName: (path) => path,
|
|
16
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
17
|
+
getNewLine: () => ts.sys.newLine,
|
|
18
|
+
};
|
|
19
|
+
const parseConfigFileHost = {
|
|
20
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
21
|
+
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
|
|
22
|
+
readDirectory: ts.sys.readDirectory,
|
|
23
|
+
fileExists: ts.sys.fileExists,
|
|
24
|
+
readFile: ts.sys.readFile,
|
|
25
|
+
onUnRecoverableConfigFileDiagnostic: reportDiagnostic,
|
|
26
|
+
};
|
|
27
|
+
const parsedConfig = ts.getParsedCommandLineOfConfigFile(configPath, { noEmitOnError: true }, parseConfigFileHost);
|
|
28
|
+
if (!parsedConfig) {
|
|
29
|
+
throw new Error("Invalid 'tsconfig.json'");
|
|
30
|
+
}
|
|
31
|
+
logger.verbose('Config found and parsed');
|
|
32
|
+
logger.verbose("We're about to create the program");
|
|
33
|
+
const compilerHost = ts.createCompilerHost(parsedConfig.options);
|
|
34
|
+
compilerHost.readFile = (0, utils_1.displayFilename)(compilerHost.readFile, 'Reading', logger);
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
compilerHost.readFile.enableDisplay();
|
|
37
|
+
const program = ts.createProgram(parsedConfig.fileNames, parsedConfig.options, compilerHost);
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
const filesCount = compilerHost.readFile.disableDisplay();
|
|
40
|
+
const allDiagnostics = ts.getPreEmitDiagnostics(program).slice();
|
|
41
|
+
logger.verbose(`Program created, read ${filesCount} files`);
|
|
42
|
+
if (!hasErrors(allDiagnostics)) {
|
|
43
|
+
logger.verbose('We finished making the program! Emitting...');
|
|
44
|
+
const transformPathsToLocalModules = (0, transformers_1.createTransformPathsToLocalModules)(ts);
|
|
45
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
46
|
+
after: [transformPathsToLocalModules],
|
|
47
|
+
afterDeclarations: [transformPathsToLocalModules],
|
|
48
|
+
});
|
|
49
|
+
logger.verbose('Emit complete!');
|
|
50
|
+
allDiagnostics.push(...emitResult.diagnostics);
|
|
51
|
+
}
|
|
52
|
+
allDiagnostics.forEach(reportDiagnostic);
|
|
53
|
+
if (hasErrors(allDiagnostics)) {
|
|
54
|
+
logger.error(`Error compile, elapsed time ${(0, pretty_time_1.elapsedTime)(start)}`);
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
logger.success(`Compiled successfully in ${(0, pretty_time_1.elapsedTime)(start)}`);
|
|
59
|
+
}
|
|
60
|
+
function reportDiagnostic(diagnostic) {
|
|
61
|
+
if (logger.isVerbose) {
|
|
62
|
+
logger.message(ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
logger.message((0, diagnostic_1.formatDiagnosticBrief)(ts, diagnostic, formatHost));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function hasErrors(diagnostics) {
|
|
69
|
+
return diagnostics.some(({ category }) => category === ts.DiagnosticCategory.Error);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.compile = compile;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.formatDiagnosticBrief = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const colors_1 = require("../logger/colors");
|
|
9
|
+
function formatDiagnosticBrief(ts, diagnostic, host) {
|
|
10
|
+
let output = '';
|
|
11
|
+
if (diagnostic.file) {
|
|
12
|
+
const { file, start } = diagnostic;
|
|
13
|
+
output += formatLocation(file, start !== null && start !== void 0 ? start : 0);
|
|
14
|
+
output += ' - ';
|
|
15
|
+
}
|
|
16
|
+
output += categoryColor(diagnostic.category)(ts.DiagnosticCategory[diagnostic.category]);
|
|
17
|
+
output += colors_1.colors.grey(` TS${diagnostic.code}: `);
|
|
18
|
+
output += ts.flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine());
|
|
19
|
+
return output;
|
|
20
|
+
function formatLocation(file, start) {
|
|
21
|
+
const { line, character } = ts.getLineAndCharacterOfPosition(file, start);
|
|
22
|
+
const filePath = path_1.default.relative(process.cwd(), file.fileName);
|
|
23
|
+
return (colors_1.colors.cyanBright(filePath) +
|
|
24
|
+
':' +
|
|
25
|
+
colors_1.colors.yellowBright(line + 1) +
|
|
26
|
+
':' +
|
|
27
|
+
colors_1.colors.yellowBright(character + 1));
|
|
28
|
+
}
|
|
29
|
+
function categoryColor(category) {
|
|
30
|
+
switch (category) {
|
|
31
|
+
case ts.DiagnosticCategory.Error: {
|
|
32
|
+
return colors_1.colors.redBright;
|
|
33
|
+
}
|
|
34
|
+
case ts.DiagnosticCategory.Warning: {
|
|
35
|
+
return colors_1.colors.yellowBright;
|
|
36
|
+
}
|
|
37
|
+
case ts.DiagnosticCategory.Suggestion: {
|
|
38
|
+
return colors_1.colors.magentaBright;
|
|
39
|
+
}
|
|
40
|
+
default: {
|
|
41
|
+
return colors_1.colors;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.formatDiagnosticBrief = formatDiagnosticBrief;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.createTransformPathsToLocalModules = void 0;
|
|
27
|
+
const path = __importStar(require("path"));
|
|
28
|
+
function createTransformPathsToLocalModules(ts) {
|
|
29
|
+
function resolveModule(module, sourceFileName, options) {
|
|
30
|
+
if (module.startsWith('.')) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const resolve = ts.resolveModuleName(module, sourceFileName, options, ts.sys);
|
|
34
|
+
if (resolve.resolvedModule && !resolve.resolvedModule.isExternalLibraryImport) {
|
|
35
|
+
const relativePath = path.relative(path.dirname(sourceFileName), resolve.resolvedModule.resolvedFileName);
|
|
36
|
+
const parsed = path.parse(relativePath);
|
|
37
|
+
const newFilePath = path.join(parsed.dir, parsed.name === 'index' ? '' : parsed.name);
|
|
38
|
+
return newFilePath.startsWith('.') ? newFilePath : `./${newFilePath}`;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
function isDynamicImport(node) {
|
|
43
|
+
return ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
44
|
+
}
|
|
45
|
+
function isRequire(node) {
|
|
46
|
+
return (ts.isCallExpression(node) &&
|
|
47
|
+
ts.isIdentifier(node.expression) &&
|
|
48
|
+
node.expression.text === 'require');
|
|
49
|
+
}
|
|
50
|
+
function transformFile(sourceFile, context) {
|
|
51
|
+
const options = context.getCompilerOptions();
|
|
52
|
+
return ts.visitNode(sourceFile, visitor);
|
|
53
|
+
function visitor(node) {
|
|
54
|
+
let modulePath;
|
|
55
|
+
if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
|
|
56
|
+
node.moduleSpecifier &&
|
|
57
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
58
|
+
modulePath = node.moduleSpecifier.text;
|
|
59
|
+
}
|
|
60
|
+
else if ((isDynamicImport(node) || isRequire(node)) &&
|
|
61
|
+
node.arguments[0] &&
|
|
62
|
+
ts.isStringLiteral(node.arguments[0])) {
|
|
63
|
+
modulePath = node.arguments[0].text;
|
|
64
|
+
}
|
|
65
|
+
else if (ts.isImportTypeNode(node) &&
|
|
66
|
+
ts.isLiteralTypeNode(node.argument) &&
|
|
67
|
+
ts.isStringLiteral(node.argument.literal)) {
|
|
68
|
+
modulePath = node.argument.literal.text;
|
|
69
|
+
}
|
|
70
|
+
if (modulePath) {
|
|
71
|
+
const resolvedPath = resolveModule(modulePath, sourceFile.fileName, options);
|
|
72
|
+
if (resolvedPath) {
|
|
73
|
+
if (ts.isImportDeclaration(node)) {
|
|
74
|
+
const newStatement = context.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, context.factory.createStringLiteral(resolvedPath), node.assertClause);
|
|
75
|
+
ts.setSourceMapRange(newStatement, ts.getSourceMapRange(node));
|
|
76
|
+
return newStatement;
|
|
77
|
+
}
|
|
78
|
+
if (ts.isExportDeclaration(node)) {
|
|
79
|
+
const newStatement = context.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, context.factory.createStringLiteral(resolvedPath), node.assertClause);
|
|
80
|
+
ts.setSourceMapRange(newStatement, ts.getSourceMapRange(node));
|
|
81
|
+
return newStatement;
|
|
82
|
+
}
|
|
83
|
+
if (isDynamicImport(node) || isRequire(node)) {
|
|
84
|
+
const newStatement = context.factory.updateCallExpression(node, node.expression, node.typeArguments, context.factory.createNodeArray([
|
|
85
|
+
context.factory.createStringLiteral(resolvedPath),
|
|
86
|
+
]));
|
|
87
|
+
ts.setSourceMapRange(newStatement, ts.getSourceMapRange(node));
|
|
88
|
+
return newStatement;
|
|
89
|
+
}
|
|
90
|
+
if (ts.isImportTypeNode(node)) {
|
|
91
|
+
const newNode = context.factory.updateImportTypeNode(node, context.factory.createLiteralTypeNode(context.factory.createStringLiteral(resolvedPath)), node.qualifier, node.typeArguments, node.isTypeOf);
|
|
92
|
+
ts.setSourceMapRange(newNode, ts.getSourceMapRange(node));
|
|
93
|
+
return newNode;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return node;
|
|
97
|
+
}
|
|
98
|
+
return ts.visitEachChild(node, visitor, context);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return function transformPathsToLocalModules(context) {
|
|
102
|
+
return (sourceFileOrBundle) => {
|
|
103
|
+
if (ts.isSourceFile(sourceFileOrBundle)) {
|
|
104
|
+
return transformFile(sourceFileOrBundle, context);
|
|
105
|
+
}
|
|
106
|
+
if (ts.isBundle(sourceFileOrBundle)) {
|
|
107
|
+
// don't transform bundles yet
|
|
108
|
+
return sourceFileOrBundle;
|
|
109
|
+
}
|
|
110
|
+
return sourceFileOrBundle;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
exports.createTransformPathsToLocalModules = createTransformPathsToLocalModules;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type Typescript from 'typescript';
|
|
2
|
+
import type { Logger } from '../logger';
|
|
3
|
+
export declare function getProjectConfig(ts: typeof Typescript, projectPath: string): string;
|
|
4
|
+
export declare function displayFilename(originalFunc: (path: string, encoding?: string) => string | undefined, operationName: string, logger: Logger): {
|
|
5
|
+
(path: string, encoding?: string | undefined): string | undefined;
|
|
6
|
+
originalFunc: (path: string, encoding?: string) => string | undefined;
|
|
7
|
+
enableDisplay(): void;
|
|
8
|
+
disableDisplay(): number;
|
|
9
|
+
};
|
|
10
|
+
export declare function onHostEvent(host: any, functionName: any, before?: (...args: any[]) => void, after?: (...args: any[]) => void): void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.onHostEvent = exports.displayFilename = exports.getProjectConfig = void 0;
|
|
4
|
+
function getProjectConfig(ts, projectPath) {
|
|
5
|
+
const configPath = ts.findConfigFile(projectPath, ts.sys.fileExists, 'tsconfig.json');
|
|
6
|
+
if (!configPath) {
|
|
7
|
+
throw new Error("Could not find a valid 'tsconfig.json'.");
|
|
8
|
+
}
|
|
9
|
+
return configPath;
|
|
10
|
+
}
|
|
11
|
+
exports.getProjectConfig = getProjectConfig;
|
|
12
|
+
function displayFilename(originalFunc, operationName, logger) {
|
|
13
|
+
let displayEnabled = false;
|
|
14
|
+
let count = 0;
|
|
15
|
+
function displayFunction(...args) {
|
|
16
|
+
count++;
|
|
17
|
+
if (displayEnabled) {
|
|
18
|
+
const fileName = args[0];
|
|
19
|
+
logger.status(logger.colors.dim(`${operationName}: ${fileName}`));
|
|
20
|
+
}
|
|
21
|
+
return originalFunc(...args);
|
|
22
|
+
}
|
|
23
|
+
displayFunction.originalFunc = originalFunc;
|
|
24
|
+
displayFunction.enableDisplay = () => {
|
|
25
|
+
count = 0;
|
|
26
|
+
if (process.stdout.isTTY && logger.isVerbose) {
|
|
27
|
+
displayEnabled = true;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
displayFunction.disableDisplay = () => {
|
|
31
|
+
if (displayEnabled) {
|
|
32
|
+
displayEnabled = false;
|
|
33
|
+
logger.clearLine();
|
|
34
|
+
}
|
|
35
|
+
return count;
|
|
36
|
+
};
|
|
37
|
+
return displayFunction;
|
|
38
|
+
}
|
|
39
|
+
exports.displayFilename = displayFilename;
|
|
40
|
+
function onHostEvent(host, functionName, before, after) {
|
|
41
|
+
const originalFunction = host[functionName];
|
|
42
|
+
host[functionName] = (...args) => {
|
|
43
|
+
if (before) {
|
|
44
|
+
before(...args);
|
|
45
|
+
}
|
|
46
|
+
const result = originalFunction && originalFunction(...args);
|
|
47
|
+
if (after) {
|
|
48
|
+
after(result);
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.onHostEvent = onHostEvent;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type Typescript from 'typescript';
|
|
2
|
+
import type { Logger } from '../logger';
|
|
3
|
+
export declare function watch(ts: typeof Typescript, projectPath: string, { logger, onAfterFilesEmitted, enableSourceMap, }: {
|
|
4
|
+
logger: Logger;
|
|
5
|
+
onAfterFilesEmitted?: () => void;
|
|
6
|
+
enableSourceMap?: boolean;
|
|
7
|
+
}): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.watch = void 0;
|
|
4
|
+
const transformers_1 = require("./transformers");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const diagnostic_1 = require("./diagnostic");
|
|
7
|
+
function watch(ts, projectPath, { logger, onAfterFilesEmitted, enableSourceMap, }) {
|
|
8
|
+
logger.message('Start compilation in watch mode');
|
|
9
|
+
logger.message(`Typescript v${ts.version}`);
|
|
10
|
+
const configPath = (0, utils_1.getProjectConfig)(ts, projectPath);
|
|
11
|
+
const createProgram = ts.createEmitAndSemanticDiagnosticsBuilderProgram;
|
|
12
|
+
const formatHost = {
|
|
13
|
+
getCanonicalFileName: (path) => path,
|
|
14
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
15
|
+
getNewLine: () => ts.sys.newLine,
|
|
16
|
+
};
|
|
17
|
+
const host = ts.createWatchCompilerHost(configPath, { noEmitOnError: false, inlineSourceMap: enableSourceMap, inlineSources: enableSourceMap }, ts.sys, createProgram, reportDiagnostic, reportWatchStatusChanged);
|
|
18
|
+
host.readFile = (0, utils_1.displayFilename)(host.readFile, 'Reading', logger);
|
|
19
|
+
(0, utils_1.onHostEvent)(host, 'createProgram', () => {
|
|
20
|
+
logger.verbose("We're about to create the program");
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
host.readFile.enableDisplay();
|
|
23
|
+
}, () => {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
const count = host.readFile.disableDisplay();
|
|
26
|
+
logger.verbose(`Program created, read ${count} files`);
|
|
27
|
+
});
|
|
28
|
+
(0, utils_1.onHostEvent)(host, 'afterProgramCreate', (program) => {
|
|
29
|
+
logger.verbose('We finished making the program! Emitting...');
|
|
30
|
+
const transformPathsToLocalModules = (0, transformers_1.createTransformPathsToLocalModules)(ts);
|
|
31
|
+
program.emit(undefined, undefined, undefined, undefined, {
|
|
32
|
+
after: [transformPathsToLocalModules],
|
|
33
|
+
afterDeclarations: [transformPathsToLocalModules],
|
|
34
|
+
});
|
|
35
|
+
logger.verbose('Emit completed!');
|
|
36
|
+
}, () => {
|
|
37
|
+
onAfterFilesEmitted === null || onAfterFilesEmitted === void 0 ? void 0 : onAfterFilesEmitted();
|
|
38
|
+
});
|
|
39
|
+
// `createWatchProgram` creates an initial program, watches files, and updates
|
|
40
|
+
// the program over time.
|
|
41
|
+
ts.createWatchProgram(host);
|
|
42
|
+
function reportDiagnostic(diagnostic) {
|
|
43
|
+
if (logger.isVerbose) {
|
|
44
|
+
logger.message(ts.formatDiagnosticsWithColorAndContext([diagnostic], formatHost));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
logger.message((0, diagnostic_1.formatDiagnosticBrief)(ts, diagnostic, formatHost));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/*
|
|
51
|
+
* Prints a diagnostic every time the watch status changes.
|
|
52
|
+
* This is mainly for messages like "Starting compilation" or "Compilation completed".
|
|
53
|
+
*/
|
|
54
|
+
function reportWatchStatusChanged(diagnostic) {
|
|
55
|
+
if (diagnostic.messageText) {
|
|
56
|
+
logger.message(ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.watch = watch;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.shouldCompileTarget = exports.createRunFolder = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const paths_1 = __importDefault(require("./paths"));
|
|
9
|
+
function createRunFolder() {
|
|
10
|
+
const runPath = paths_1.default.appRun;
|
|
11
|
+
if (!fs_1.default.existsSync(runPath)) {
|
|
12
|
+
fs_1.default.mkdirSync(runPath, { recursive: true });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.createRunFolder = createRunFolder;
|
|
16
|
+
function shouldCompileTarget(target, targetName) {
|
|
17
|
+
return target === undefined || target === targetName;
|
|
18
|
+
}
|
|
19
|
+
exports.shouldCompileTarget = shouldCompileTarget;
|