-
Notifications
You must be signed in to change notification settings - Fork 5
/
publish-packages.js
157 lines (131 loc) · 4.17 KB
/
publish-packages.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const path = require('path');
const fs = require('fs');
const spawnSync = require('child_process').spawnSync;
const PackageSource = require('./tools-imports.js').PackageSource;
// During publishing packages, this handles:
//
// 1) Ensuring published packages have .d.ts and package-types.json files
// 2) Generate types if needed
// 3) Uninstall npm dev dependencies, and reinstall them after publishing
Plugin.registerCompiler({
// These aren't used by the compiler
// This ensures meteor includes these files when building or publishing
// the package
extensions: ['d.ts', 'd.ts.map'],
filenames: ['package-types.json']
}, () => new PackageCompiler());
class PackageCompiler {
processFilesForTarget(files) {
// Do nothing
// Meteor Typescript compilers usually ignore .d.ts files
// and package-types.json are not used during runtime
}
}
// Probably isn't completely reliable, but is close enough
const isPublish = process.argv.includes('publish');
const packagePath = process.cwd();
let prepared = false;
const originalFindSources = PackageSource.prototype._findSources;
PackageSource.prototype._findSources = function (options) {
let sourceProcessorSet = options.sourceProcessorSet;
if (
isPublish &&
!prepared &&
this.sourceRoot === packagePath &&
this.name !== 'zodern:types' &&
sourceProcessorSet &&
// If _findSources is called for a linter, then _allowConflicts will be true
// and we can't call getByFilename
!sourceProcessorSet._allowConflicts &&
sourceProcessorSet.getByFilename &&
sourceProcessorSet.getByFilename('package-types.json')
) {
preparePackageForPublish(this.sourceRoot);
prepared = true;
}
return originalFindSources.apply(this, arguments);
}
function preparePackageForPublish(packageDir) {
const packageTypesPath = path.resolve(packageDir, 'package-types.json');
const content = fs.readFileSync(packageTypesPath);
const config = JSON.parse(content);
if (!config.typesEntry) {
throw new Error('Package\'s package-types.json file does not have an typesEntry option');
}
let typesEntryPath = path.resolve(
packagePath,
config.typesEntry
);
if (typesEntryPath.endsWith('.d.ts')) {
console.log('[zodern:types] => Skipping types generation since typesEntry points to a .d.ts file');
if (!fs.existsSync(typesEntryPath)) {
throw `typesEntry file does not exit: ${typesEntryPath}`;
}
} else {
generateTypes(packageDir, typesEntryPath);
}
// Remove dev npm dependencies
console.log('[zodern:types] => Uninstalling package\'s dev dependencies');
let npmResult = spawnSync('npm', ['prune', '--production'], {
cwd: packageDir,
stdio: 'inherit'
});
if (npmResult.status > 0) {
throw new Error('Failed uninstalling production npm deps');
}
process.on('exit', () => {
console.log('[zodern:types] => Re-installing package\'s dev dependencies');
const env = JSON.parse(JSON.stringify(process.env));
env.NODE_ENV = 'development';
spawnSync('npm', ['install'], {
cwd: packageDir,
stdio: 'inherit',
env: env
});
});
}
function generateTypes(packageDir, typesEntryPath) {
console.log('[zodern:types] => Cleaning old generated types');
const typesPath = path.resolve(packageDir, '__types');
if (fs.existsSync(typesPath)) {
fs.rmSync(typesPath, {
recursive: true
});
}
// Generate types
const args = [
'--no-install',
'tsc',
'--declaration',
'--emitDeclarationOnly',
'--noEmit',
'false',
'--declarationDir',
typesPath,
'--declarationMap',
'--rootDir',
'./'
]
console.log('[zodern:types] => Generating package type declaration files');
const result = spawnSync('npx', args, {
cwd: packageDir,
stdio: 'inherit',
});
if (result.status > 0) {
console.log('Ran: ', 'npx', args.join(' '))
throw 'Generating package types failed';
}
fs.writeFileSync(
path.resolve(typesPath, 'package-types.json'),
JSON.stringify({
typesEntry: path.relative(packageDir, typesEntryPath).replace('.ts', '.d.ts')
}, null, 2),
'utf-8'
);
fs.writeFileSync(
path.resolve(typesPath, '.gitignore'),
'*',
'utf-8'
);
}