forked from laurent22/joplin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
157 lines (126 loc) · 4.26 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
const gulp = require('gulp');
const ts = require('gulp-typescript');
const fs = require('fs-extra');
const toolUtils = {};
toolUtils.isLinux = () => {
return process && process.platform === 'linux';
};
toolUtils.isWindows = () => {
return process && process.platform === 'win32';
};
toolUtils.isMac = () => {
return process && process.platform === 'darwin';
};
toolUtils.execCommand = function(command) {
const exec = require('child_process').exec;
return new Promise((resolve, reject) => {
exec(command, (error, stdout) => {
if (error) {
if (error.signal == 'SIGTERM') {
resolve('Process was killed');
} else {
reject(error);
}
} else {
resolve(stdout.trim());
}
});
});
};
const pathUtils = {};
pathUtils.dirname = function(path) {
if (!path) throw new Error('Path is empty');
let s = path.split(/\/|\\/);
s.pop();
return s.join('/');
};
pathUtils.basename = function(path) {
if (!path) throw new Error('Path is empty');
let s = path.split(/\/|\\/);
return s[s.length - 1];
};
pathUtils.filename = function(path, includeDir = false) {
if (!path) throw new Error('Path is empty');
let output = includeDir ? path : pathUtils.basename(path);
if (output.indexOf('.') < 0) return output;
output = output.split('.');
output.pop();
return output.join('.');
};
pathUtils.fileExtension = function(path) {
if (!path) throw new Error('Path is empty');
let output = path.split('.');
if (output.length <= 1) return '';
return output[output.length - 1];
};
toolUtils.getAllFiles = function(dir, options = null) {
var results = [];
var list = fs.readdirSync(dir);
list.forEach(function(file) {
file = `${dir}/${file}`;
const filename = pathUtils.basename(file);
const ext = pathUtils.fileExtension(file).toLowerCase();
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
if (file.indexOf('ElectronClient/app/lib') >= 0) return;
if (file.indexOf('CliClient/lib') >= 0) return;
if (filename === 'node_modules' || filename === '.git' || filename === 'build' || filename === 'tests-build' || filename === 'dist') return;
results = results.concat(toolUtils.getAllFiles(file, options));
} else {
let addIt = true;
if (options.extensions && options.extensions.length && !options.extensions.includes(ext)) {
addIt = false;
}
if (addIt) results.push(file.substr(__dirname.length + 1));
}
});
return results;
};
async function replaceFileText(filePath, regex, toInsert) {
const content = await fs.readFile(filePath, 'utf8');
const newContent = content.replace(regex, toInsert);
await fs.writeFile(filePath, newContent);
}
const tsProject = ts.createProject('tsconfig.json');
const tscTaskSrc = [
'ReactNativeClient/**/*.tsx',
'ReactNativeClient/**/*.ts',
'ElectronClient/**/*.tsx',
'ElectronClient/**/*.ts',
'CliClient/**/*.tsx',
'CliClient/**/*.ts',
];
const tscTask = function() {
return tsProject.src()
.pipe(tsProject())
.js.pipe(gulp.dest('./'));
};
const copyLibSrc = 'ReactNativeClient/lib/**/*';
const copyLibTask = async function() {
if (toolUtils.isWindows()) {
await toolUtils.execCommand(`xcopy /C /I /H /R /Y /S "${__dirname}\\ReactNativeClient\\lib" ElectronClient\\app\\lib`);
} else {
await toolUtils.execCommand(`rsync -a --delete "${__dirname}/ReactNativeClient/lib/" "ElectronClient/app/lib/"`);
await toolUtils.execCommand(`rsync -a --delete "${__dirname}/ReactNativeClient/lib/" "CliClient/build/lib/"`);
}
};
const updateIgnoredTypeScriptBuildTask = async function() {
const tsFiles = toolUtils.getAllFiles(__dirname, { extensions: ['tsx', 'ts'] });
const ignoredFiles = tsFiles.map(f => {
const s = f.split('.');
s.pop();
return `${s.join('.')}.js`;
});
const regex = /(# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD)[\s\S]*(# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD)/;
const replacement = `$1\n${ignoredFiles.join('\n')}\n$2`;
await replaceFileText(`${__dirname}/.gitignore`, regex, replacement);
await replaceFileText(`${__dirname}/.eslintignore`, regex, replacement);
};
gulp.task('tsc', tscTask);
gulp.task('copyLib', copyLibTask);
gulp.task('watch', function() {
gulp.watch(tscTaskSrc, tscTask);
gulp.watch(copyLibSrc, copyLibTask);
});
gulp.task('build', gulp.series(tscTask, copyLibTask));
gulp.task('updateIgnoredTypeScriptBuild', updateIgnoredTypeScriptBuildTask);