Skip to content

Commit

Permalink
Improving build
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Feb 17, 2020
1 parent ca9102d commit 4c97aa8
Show file tree
Hide file tree
Showing 7 changed files with 4,051 additions and 174 deletions.
5 changes: 3 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ ReactNativeClient/pluginAssets/
ReactNativeClient/lib/joplin-renderer/vendor/fountain.min.js
ReactNativeClient/lib/joplin-renderer/assets/

# Ignore files generated from TypeScript files
# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD
ElectronClient/app/gui/ShareNoteDialog.js
ReactNativeClient/lib/JoplinServerApi.js
ReactNativeClient/PluginAssetsLoader.js
ReactNativeClient/lib/JoplinServerApi.js
ReactNativeClient/lib/joplin-renderer/MdToHtml/rules/mermaid.js
ReactNativeClient/lib/joplin-renderer/MdToHtml/rules/sanitize_html.js
# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ Tools/commit_hook.txt
.vscode/*
*.map

# Ignore files generated from TypeScript files
# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD
ElectronClient/app/gui/ShareNoteDialog.js
ReactNativeClient/lib/JoplinServerApi.js
ReactNativeClient/PluginAssetsLoader.js
ReactNativeClient/lib/JoplinServerApi.js
ReactNativeClient/lib/joplin-renderer/MdToHtml/rules/mermaid.js
ReactNativeClient/lib/joplin-renderer/MdToHtml/rules/sanitize_html.js
# AUTO-GENERATED - EXCLUDED TYPESCRIPT BUILD
56 changes: 10 additions & 46 deletions ElectronClient/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ElectronClient/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@
"node-notifier": "^6.0.0",
"promise": "^8.0.1",
"query-string": "^5.1.1",
"react": "^16.12.0",
"react": "^16.9.0",
"react-ace": "^6.1.4",
"react-datetime": "^2.14.0",
"react-dom": "^16.12.0",
"react-dom": "^16.9.0",
"react-redux": "^5.0.7",
"react-select": "^2.4.3",
"react-tooltip": "^3.10.0",
Expand Down
157 changes: 157 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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);
Loading

0 comments on commit 4c97aa8

Please sign in to comment.