-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
167 lines (147 loc) · 4.97 KB
/
gulpfile.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
158
159
160
161
162
163
164
165
166
167
const gulp = require('gulp');
const gulpSequence = require('gulp-sequence');
const webpack = require('webpack-stream');
const { spawnSync } = require('child_process');
const { readJSONSync } = require('fs-extra');
const { platform } = require('os');
const { join, dirname, basename, relative } = require('path');
const { readdirSync, mkdirSync, existsSync, rmdirSync, statSync } = require('fs');
const extensions = [];
const extPath = join(__dirname, './extensions');
const distPath = join(__dirname, './out');
const initTask = (ext) => `${ext}-init`;
const clientTask = (ext) => `${ext}-client`;
const serverTask = (ext) => `${ext}-server`;
const installTask = (ext) => `${ext}-install`;
function mkdist() {
if (!existsSync(distPath)) {
mkdirSync(distPath);
} else {
spawnSync('rm', [ '-rf', distPath]);
mkdirSync(distPath);
}
}
function initExtensions () {
readdirSync('./extensions')
.forEach((ext) => {
const local = join(extPath, ext);
if (statSync(local).isDirectory())
extensions.push(ext);
})
}
initExtensions();
const commonConfig = {
output: {
libraryTarget: "umd",
},
module: {
loaders: [
{ test: /\.js$/, loader: 'babel-loader' },
],
},
externals(context, request, callback) {
let isExternals = false;
const vendor = ['vscode-textmate', 'eslint', 'typescript', 'vscode', 'jsonc-parser'];
if (vendor.indexOf(request) > -1 || request.indexOf('typescript') > -1 || request.indexOf('vscode') > -1 || request.indexOf('eslint') > -1) {
isExternals = request;
}
callback(null, isExternals);
},
resolve: {
extensions: ['.js', '.json'],
},
target: 'electron',
}
// init
extensions.forEach((ext) => {
mkdist();
gulp.task(initTask(ext), () => {
const local = join(extPath, ext);
const localTo = join(distPath, ext);
const package = join(local, 'package.json');
return gulp.src(['./extensions/**/*.json', './extensions/**/*.tmLanguage', '!**/node_modules/**'])
.pipe(gulp.dest(distPath))
})
})
// client compile
extensions.forEach((ext) => {
gulp.task(clientTask(ext), [initTask(ext)], () => {
const local = join(extPath, ext);
const packageJson = readJSONSync(join(local, 'package.json'));
if (!packageJson.main) {
return gulp.src(local)
gulp.dest(distPath)
}
// client entry
let client = join(local, packageJson.main);
const localDist = join(distPath, ext, packageJson.main);
if (client.indexOf('.js') === -1)
client += '.js';
commonConfig.output.filename = basename(client);
commonConfig.output.path = dirname(client);
// FIX ME for eslint and ts
if (ext === 'typescript') {
return gulp.src(`${dirname(client)}/**`)
.pipe(gulp.dest(dirname(client.replace('extensions', 'out'))))
} else if (ext === 'eslint') {
return gulp.src(relative(__dirname, client))
.pipe(gulp.dest(dirname(client.replace('extensions', 'out'))))
} else {
return gulp.src(relative(__dirname, client))
.pipe(webpack(commonConfig))
.pipe(gulp.dest(dirname(client.replace('extensions', 'out'))))
}
})
});
// server compile
extensions.forEach((ext) => {
gulp.task(serverTask(ext), [clientTask(ext)], () => {
let entry = '';
const local = join(extPath, ext);
const packageJson = readJSONSync(join(local, 'package.json'));
// server entry
const serverRoot = join(local, 'server');
if (existsSync(serverRoot) && statSync(serverRoot).isDirectory()) {
let serverScript = join(serverRoot, 'server.js');
if (existsSync(serverScript) && statSync(serverScript).isFile()) {
entry = serverScript;
} else {
const serverScript = join(serverRoot, 'out', `${ext}ServerMain.js`);
if (existsSync(serverScript) && statSync(serverScript).isFile())
entry = serverScript;
}
}
if (!packageJson.main || !entry) {
return gulp.src(local)
gulp.dest(distPath)
}
if (entry.indexOf('.js') === -1)
entry += '.js';
commonConfig.output.filename = basename(entry);
commonConfig.output.path = dirname(entry);
if (ext === 'eslint') {
return gulp.src(relative(__dirname, entry))
.pipe(gulp.dest(dirname(entry.replace('extensions', 'out'))))
} else {
return gulp.src(relative(__dirname, entry))
.pipe(webpack(commonConfig))
.pipe(gulp.dest(dirname(entry.replace('extensions', 'out'))))
}
})
});
// install deps
extensions.forEach((ext) => {
gulp.task(installTask(ext), () => {
const ret = spawnSync((platform() === 'win32') ? 'npm.cmd' : 'tnpm', ['i'], {
cwd: `./extensions/${ext}`,
})
if (existsSync(`./extensions/${ext}/server`)) {
const ret = spawnSync((platform() === 'win32') ? 'npm.cmd' : 'tnpm', ['i'], {
cwd: `./extensions/${ext}/server`,
})
}
return gulp.src(join(extPath, ext));
})
});
gulp.task('build', gulpSequence(...extensions.map(serverTask)));
gulp.task('install', gulpSequence(...extensions.map(installTask)));