2018-04-25 03:40:18 +08:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const child_process = require('child_process');
|
2019-11-20 23:00:19 +08:00
|
|
|
let buildingInProgress = false;
|
|
|
|
let currentBuildingModule = '';
|
2018-04-25 03:40:18 +08:00
|
|
|
|
|
|
|
const yarnBuildTask = {
|
|
|
|
shouldBuild(resourceName) {
|
|
|
|
try {
|
|
|
|
const resourcePath = GetResourcePath(resourceName);
|
|
|
|
|
|
|
|
const packageJson = path.resolve(resourcePath, 'package.json');
|
|
|
|
const yarnLock = path.resolve(resourcePath, 'yarn.lock');
|
|
|
|
|
|
|
|
const packageStat = fs.statSync(packageJson);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const yarnStat = fs.statSync(yarnLock);
|
2018-10-03 22:55:18 +08:00
|
|
|
|
2018-04-25 03:40:18 +08:00
|
|
|
if (packageStat.mtimeMs > yarnStat.mtimeMs) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// no yarn.lock, but package.json - install time!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
build(resourceName, cb) {
|
2019-11-20 23:00:19 +08:00
|
|
|
let buildYarn = async () => {
|
|
|
|
while (buildingInProgress) {
|
|
|
|
console.log(`yarn is busy by another process: we are waiting to compile ${resourceName}`);
|
|
|
|
await sleep(3000);
|
|
|
|
}
|
|
|
|
buildingInProgress = true;
|
|
|
|
currentBuildingModule = resourceName;
|
|
|
|
const process = child_process.fork(
|
|
|
|
require.resolve('./yarn_cli.js'),
|
2019-12-10 17:57:04 +08:00
|
|
|
['install', '--ignore-scripts'],
|
2019-11-20 23:00:19 +08:00
|
|
|
{
|
|
|
|
cwd: path.resolve(GetResourcePath(resourceName))
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', (code, signal) => {
|
|
|
|
setImmediate(() => {
|
|
|
|
if (code != 0 || signal) {
|
|
|
|
buildingInProgress = false;
|
|
|
|
currentBuildingModule = '';
|
|
|
|
cb(false, 'yarn failed!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const resourcePath = GetResourcePath(resourceName);
|
|
|
|
const yarnLock = path.resolve(resourcePath, 'yarn.lock');
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.utimesSync(yarnLock, new Date(), new Date());
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
buildingInProgress = false;
|
|
|
|
currentBuildingModule = '';
|
|
|
|
cb(true);
|
|
|
|
});
|
2018-04-25 03:40:18 +08:00
|
|
|
});
|
2019-11-20 23:00:19 +08:00
|
|
|
};
|
|
|
|
buildYarn().then();
|
2018-04-25 03:40:18 +08:00
|
|
|
}
|
2019-11-20 23:00:19 +08:00
|
|
|
};
|
2018-04-25 03:40:18 +08:00
|
|
|
|
2019-11-20 23:00:19 +08:00
|
|
|
function sleep(ms) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
RegisterResourceBuildTaskFactory('yarn', () => yarnBuildTask);
|