/
workspace.js
84 lines (68 loc) · 2.67 KB
/
workspace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
const fs = require('fs-extra');
const { path, cwd } = require('../../../../lib/platform/path');
const createConfig = require('./configuration');
async function getTypescriptDirectory() {
if (await fs.pathExists(path.join(cwd(), '../saby-typescript'))) {
return path.join(cwd(), '../saby-typescript');
}
return path.join(cwd(), 'node_modules/saby-typescript');
}
function xor(a, b) {
return (!a && b) || (a && !b);
}
async function removeDropCache(taskParameters) {
if (!taskParameters.config.tscCache) {
return;
}
// В случае, если кеш-файл существует, а output директории нет,
// необходимо удалить кеш-файл
const cacheExists = await fs.pathExists(taskParameters.config.tscCachePath);
const outputExists = await fs.pathExists(taskParameters.typescriptOutputDir);
if (xor(cacheExists, outputExists)) {
await fs.promises.rm(taskParameters.config.tscCachePath, { force: true, recursive: true });
await fs.promises.rm(taskParameters.typescriptOutputDir, { force: true, recursive: true });
}
}
function prepare(taskParameters) {
return async function prepareTypescriptWorkspace() {
taskParameters.sabyTypescriptDir = await getTypescriptDirectory();
taskParameters.typescriptOutputDir = path.join(
path.dirname(taskParameters.config.tscCachePath),
'emit'
);
taskParameters.typescriptConfigPath = path.join(
taskParameters.config.sourcesDirectory,
'tsconfig.json'
);
await removeDropCache(taskParameters);
const config = createConfig(taskParameters);
await fs.outputFile(
taskParameters.typescriptConfigPath,
JSON.stringify(config, null, 3)
);
await fs.ensureSymlink(
path.join(taskParameters.sabyTypescriptDir, 'tslib.d.ts'),
path.join(taskParameters.config.sourcesDirectory, 'tslib.d.ts')
);
/**
* symlink also node_modules from builder to current project.
* tsconfig requires types definition module(node_modules/@types) to be defined in current project node_modules.
*/
await fs.ensureSymlink(
path.dirname(taskParameters.sabyTypescriptDir),
path.join(taskParameters.config.sourcesDirectory, 'node_modules')
);
};
}
function clean(taskParameters) {
return async function cleanTypescriptWorkspace() {
await fs.unlink(path.join(taskParameters.config.sourcesDirectory, 'tslib.d.ts'));
await fs.unlink(path.join(taskParameters.config.sourcesDirectory, 'node_modules'));
delete taskParameters.sabyTypescriptDir;
};
}
module.exports = {
prepare,
clean
};