Skip to content

Use async in scripts #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion js/scripts/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "script"
"sourceType": "script",
"ecmaVersion": 2017
},
"rules": {
"indent": [
Expand Down
12 changes: 4 additions & 8 deletions js/scripts/generate-enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,8 @@ function writeJavascriptFile() {
return fse.outputFile(jsEnumDst, content);
}

function createJavascriptFiles() {
return new Promise(function(resolve) {
resolve(writeJavascriptFile());
});
async function createJavascriptFiles() {
return await writeJavascriptFile();
}

function writePythonFile() {
Expand Down Expand Up @@ -144,10 +142,8 @@ function writePythonFile() {
return fse.outputFile(pyEnumDst, content);
}

function createPythonFiles() {
return new Promise(function(resolve) {
resolve(writePythonFile());
});
async function createPythonFiles() {
return await writePythonFile();
}

function generateFiles() {
Expand Down
18 changes: 9 additions & 9 deletions js/scripts/generate-shader-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function mapPromiseFnOverObject(object, mapFn) {
}


function createPythonWrapper(name, relativePath) {
async function createPythonWrapper(name, relativePath) {

const data = THREE[name];
let serialized;
Expand Down Expand Up @@ -92,14 +92,14 @@ function createPythonWrapper(name, relativePath) {
generatorScriptName: path.basename(__filename),
});
promises.push(fse.outputFile(pyPath, output));
return Promise.all(promises);
await Promise.all(promises);
}

function createPythonModuleInitFile(modulePath) {
async function createPythonModuleInitFile(modulePath) {

const dirname = path.dirname(modulePath);
const pyInitFilePath = path.resolve(pySrcDir, dirname, '__init__.py');
return fse.ensureFile(pyInitFilePath);
await fse.ensureFile(pyInitFilePath);

}

Expand All @@ -110,12 +110,12 @@ function createPythonFiles() {
return Promise.resolve();
}

return mapPromiseFnOverObject(shaderUtilsConfig, function(name, configObj) {
return mapPromiseFnOverObject(shaderUtilsConfig, async function(name, configObj) {
const relativePath = configObj.relativePath;
return createPythonWrapper(name, relativePath).then(function() {
// ensures each dir has empty __init__.py file for proper importing of sub dirs
return createPythonModuleInitFile(relativePath);
});
await createPythonWrapper(name, relativePath);

// ensures each dir has empty __init__.py file for proper importing of sub dirs
await createPythonModuleInitFile(relativePath);
});
}

Expand Down
104 changes: 51 additions & 53 deletions js/scripts/generate-wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,77 +591,75 @@ function writeJavascriptIndexFiles() {
// Regexp's
const RE_AUTOGEN_EXT = /\.autogen\.js$/;

function writeIndexForDir(dirPath, isTopLevel) {
async function writeIndexForDir(dirPath, isTopLevel) {

const dirAbsPath = path.resolve(jsSrcDir, dirPath);

// Generate list of files in dir to include in index.js as require lines
return fse.readdir(dirAbsPath).then(function(dirFiles) {
let dirFiles = await fse.readdir(dirAbsPath);

// get proper relative path for file
dirFiles = dirFiles.map(function(filename) {
return './' + path.join(dirPath, filename);
});
// get proper relative path for file
dirFiles = dirFiles.map(filename => {
return './' + path.join(dirPath, filename);
});

// filter excluded files
dirFiles = dirFiles.filter(function(filePath) {
// filter excluded files
dirFiles = dirFiles.filter(filePath => {

// ignore autogen files in _base dir
if (/_base/.test(dirPath) && RE_AUTOGEN_EXT.test(filePath)) {
return false;
}
// ignore autogen files in _base dir
if (/_base/.test(dirPath) && RE_AUTOGEN_EXT.test(filePath)) {
return false;
}

// compare filePath to each exclude pattern
const shouldExclude = _.any(excludes, function(testPattern) {
if (testPattern instanceof RegExp) {
return testPattern.test(filePath);
} else if (typeof testPattern === 'string') {
return testPattern === filePath;
}
});
if (shouldExclude) {
return false;
// compare filePath to each exclude pattern
const shouldExclude = _.any(excludes, function(testPattern) {
if (testPattern instanceof RegExp) {
return testPattern.test(filePath);
} else if (typeof testPattern === 'string') {
return testPattern === filePath;
}
});
if (shouldExclude) {
return false;
}

// if override class exists, load it in favor of the autogen file
// e.g. for WebGLRenderer.js, Object3D.js, DataTexture.js
// override classes should extend the autogen versions
if (RE_AUTOGEN_EXT.test(filePath)) {

const dirname = path.dirname(filePath);
const basename = path.basename(filePath, JS_AUTOGEN_EXT);
const overrideName = basename + '.js';
const overridePath = './' + path.join(dirname, overrideName);
// if override class exists, load it in favor of the autogen file
// e.g. for WebGLRenderer.js, Object3D.js, DataTexture.js
// override classes should extend the autogen versions
if (RE_AUTOGEN_EXT.test(filePath)) {

// override file present, so don't include autogen file in index
if (dirFiles.indexOf(overridePath) > -1) {
console.log('override exists for: ' + filePath);
return false;
}
const dirname = path.dirname(filePath);
const basename = path.basename(filePath, JS_AUTOGEN_EXT);
const overrideName = basename + '.js';
const overridePath = './' + path.join(dirname, overrideName);

// override file present, so don't include autogen file in index
if (dirFiles.indexOf(overridePath) > -1) {
console.log('override exists for: ' + filePath);
return false;
}

return true;
});
}

// convert file paths relative to js src dir to paths relative to dirPath
dirFiles = dirFiles.map(function(filePath) {
return './' + path.basename(filePath);
});
return true;
});

// render template
const context = {
now: new Date(),
generatorScriptName: path.basename(__filename),
top_level: isTopLevel,
submodules: dirFiles,
};
const output = jsIndexTemplate(context);
const outputPath = path.resolve(jsSrcDir, dirPath, 'index.js');
// convert file paths relative to js src dir to paths relative to dirPath
dirFiles = dirFiles.map(function(filePath) {
return './' + path.basename(filePath);
});

return fse.outputFile(outputPath, output);
// render template
const context = {
now: new Date(),
generatorScriptName: path.basename(__filename),
top_level: isTopLevel,
submodules: dirFiles,
};
const output = jsIndexTemplate(context);
const outputPath = path.resolve(jsSrcDir, dirPath, 'index.js');

});
return fse.outputFile(outputPath, output);
}

// map over all directories in js src dir
Expand Down
2 changes: 1 addition & 1 deletion js/scripts/post-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function copyExampleImagesToDocs() {
await fse.copy(
path.resolve(exampleImagesSrcDir, filePath),
path.resolve(exampleImagesDstDir, filePath)
)
);
console.log(`Copied ${filePath} to docs examples' image folder`);
}));
}
Expand Down