@touk/federated-types 1.3.0-beta.1 → 1.3.0-beta.2
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/cli.js +33 -17
- package/package.json +2 -1
package/cli.js
CHANGED
|
@@ -4,6 +4,7 @@ const path = require('path');
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const findNodeModules = require('find-node-modules');
|
|
6
6
|
const ts = require('typescript');
|
|
7
|
+
const chalk = require('chalk');
|
|
7
8
|
|
|
8
9
|
const formatHost = {
|
|
9
10
|
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
@@ -12,8 +13,8 @@ const formatHost = {
|
|
|
12
13
|
|
|
13
14
|
function reportDiagnostic(diagnostic) {
|
|
14
15
|
console.log(
|
|
15
|
-
'TS Error',
|
|
16
|
-
diagnostic.code,
|
|
16
|
+
chalk.red('TS Error'),
|
|
17
|
+
chalk.yellow(diagnostic.code),
|
|
17
18
|
':',
|
|
18
19
|
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())
|
|
19
20
|
);
|
|
@@ -40,7 +41,7 @@ const getAllArgs = (argName) => {
|
|
|
40
41
|
results.push([process.argv[i + 1], process.argv[i + 2]]);
|
|
41
42
|
i += 3; // skip argName, key, value
|
|
42
43
|
} else {
|
|
43
|
-
console.error(
|
|
44
|
+
console.error(chalk.red('ERROR:'), '--exposes requires two arguments (key path)');
|
|
44
45
|
process.exit(1);
|
|
45
46
|
}
|
|
46
47
|
} else {
|
|
@@ -66,6 +67,10 @@ const configPath = configPathArg ? path.resolve(configPathArg) : null;
|
|
|
66
67
|
|
|
67
68
|
const nameArg = getArg('--name');
|
|
68
69
|
const exposesArgs = getAllArgs('--exposes');
|
|
70
|
+
const verboseArg = hasArg('--verbose') || process.env.npm_config_loglevel === 'verbose';
|
|
71
|
+
|
|
72
|
+
const log = (...args) => console.log(...args);
|
|
73
|
+
const logDebug = (...args) => verboseArg && console.debug(chalk.gray.dim(...args));
|
|
69
74
|
|
|
70
75
|
const findFederationConfig = (base) => {
|
|
71
76
|
let files = fs.readdirSync(base);
|
|
@@ -92,12 +97,18 @@ let federationConfig;
|
|
|
92
97
|
if (nameArg || exposesArgs.length > 0) {
|
|
93
98
|
// Inline config mode
|
|
94
99
|
if (!nameArg || exposesArgs.length === 0) {
|
|
95
|
-
console.error(
|
|
100
|
+
console.error(
|
|
101
|
+
chalk.red('ERROR:'),
|
|
102
|
+
'Both --name and --exposes are required for inline config'
|
|
103
|
+
);
|
|
96
104
|
process.exit(1);
|
|
97
105
|
}
|
|
98
106
|
|
|
99
107
|
if (configPath) {
|
|
100
|
-
console.warn(
|
|
108
|
+
console.warn(
|
|
109
|
+
chalk.yellow('WARNING:'),
|
|
110
|
+
'Both inline config and --config provided. Using inline config.'
|
|
111
|
+
);
|
|
101
112
|
}
|
|
102
113
|
|
|
103
114
|
const exposes = {};
|
|
@@ -113,22 +124,26 @@ if (nameArg || exposesArgs.length > 0) {
|
|
|
113
124
|
exposes: exposes,
|
|
114
125
|
};
|
|
115
126
|
|
|
116
|
-
|
|
127
|
+
log('Using inline config');
|
|
128
|
+
logDebug(JSON.stringify(federationConfig, null, 2));
|
|
117
129
|
} else {
|
|
118
130
|
// File-based config (current behavior)
|
|
119
131
|
if (configPath && !fs.existsSync(configPath)) {
|
|
120
|
-
console.error(
|
|
132
|
+
console.error(chalk.red('ERROR:'), `Unable to find a provided config: ${configPath}`);
|
|
121
133
|
process.exit(1);
|
|
122
134
|
}
|
|
123
135
|
|
|
124
136
|
const federationConfigPath = configPath || findFederationConfig('./');
|
|
125
137
|
|
|
126
138
|
if (federationConfigPath === undefined) {
|
|
127
|
-
console.error(
|
|
139
|
+
console.error(
|
|
140
|
+
chalk.red('ERROR:'),
|
|
141
|
+
'Unable to find a federation.config.json file in this package'
|
|
142
|
+
);
|
|
128
143
|
process.exit(1);
|
|
129
144
|
}
|
|
130
145
|
|
|
131
|
-
|
|
146
|
+
log('Using config file', chalk.blue.bold(federationConfigPath));
|
|
132
147
|
federationConfig = require(federationConfigPath);
|
|
133
148
|
}
|
|
134
149
|
|
|
@@ -141,6 +156,7 @@ function getModuleDeclareName(exposeName) {
|
|
|
141
156
|
}
|
|
142
157
|
|
|
143
158
|
try {
|
|
159
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
144
160
|
const outFile = path.resolve(outputDir, `${federationConfig.name}.d.ts`);
|
|
145
161
|
if (fs.existsSync(outFile)) {
|
|
146
162
|
fs.unlinkSync(outFile);
|
|
@@ -200,23 +216,23 @@ try {
|
|
|
200
216
|
outputDirs.forEach((_outputDir) => {
|
|
201
217
|
const _outFile = path.resolve(_outputDir, `${federationConfig.name}.d.ts`);
|
|
202
218
|
|
|
203
|
-
|
|
219
|
+
logDebug('writing typing file:', _outFile);
|
|
204
220
|
fs.mkdirSync(path.dirname(_outFile), { recursive: true });
|
|
205
221
|
fs.writeFileSync(_outFile, typing);
|
|
206
222
|
|
|
207
|
-
|
|
223
|
+
logDebug(`using output dir: ${_outputDir}`);
|
|
208
224
|
// if we are writing to the node_modules/@types directory, add a package.json file
|
|
209
225
|
if (_outputDir.includes(path.join('node_modules', '@types'))) {
|
|
210
226
|
const packageJsonPath = path.resolve(_outputDir, 'package.json');
|
|
211
227
|
|
|
212
228
|
if (!fs.existsSync(packageJsonPath)) {
|
|
213
|
-
|
|
229
|
+
logDebug('writing package.json:', packageJsonPath);
|
|
214
230
|
fs.copyFileSync(
|
|
215
231
|
path.resolve(__dirname, 'typings.package.tmpl.json'),
|
|
216
232
|
packageJsonPath
|
|
217
233
|
);
|
|
218
234
|
} else {
|
|
219
|
-
|
|
235
|
+
logDebug(packageJsonPath, 'already exists');
|
|
220
236
|
}
|
|
221
237
|
}
|
|
222
238
|
|
|
@@ -225,18 +241,18 @@ try {
|
|
|
225
241
|
const importStatement = `export * from './${federationConfig.name}';`;
|
|
226
242
|
|
|
227
243
|
if (!fs.existsSync(indexPath)) {
|
|
228
|
-
|
|
244
|
+
logDebug('creating index.d.ts file');
|
|
229
245
|
fs.writeFileSync(indexPath, `${importStatement}\n`);
|
|
230
246
|
} else {
|
|
231
|
-
|
|
247
|
+
logDebug('updating index.d.ts file');
|
|
232
248
|
const contents = fs.readFileSync(indexPath);
|
|
233
249
|
if (!contents.includes(importStatement)) {
|
|
234
250
|
fs.writeFileSync(indexPath, `${contents}${importStatement}\n`);
|
|
235
251
|
}
|
|
236
252
|
}
|
|
237
253
|
});
|
|
238
|
-
|
|
254
|
+
log(chalk.green('✓'), `Generated types for ${chalk.blue.bold(federationConfig.name)}`);
|
|
239
255
|
} catch (e) {
|
|
240
|
-
console.error(
|
|
256
|
+
console.error(chalk.red('ERROR:'), e);
|
|
241
257
|
process.exit(1);
|
|
242
258
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@touk/federated-types",
|
|
3
3
|
"description": "share typings for your federated typescript packages around your mono-repo",
|
|
4
|
-
"version": "1.3.0-beta.
|
|
4
|
+
"version": "1.3.0-beta.2",
|
|
5
5
|
"author": "Julian Wielga <j@touk.pl>",
|
|
6
6
|
"bin": {
|
|
7
7
|
"make-federated-types": "./cli.js"
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"Jonathan Greene <jgreene@pixability.com>"
|
|
11
11
|
],
|
|
12
12
|
"dependencies": {
|
|
13
|
+
"chalk": "^4.1.2",
|
|
13
14
|
"find-node-modules": "2.1.3"
|
|
14
15
|
},
|
|
15
16
|
"devDependencies": {
|