Skip to content

Commit 90ed39f

Browse files
authored
Merge pull request #260 from vidartf/async-scripts
Use async in scripts
2 parents 117dfaa + 84751a6 commit 90ed39f

File tree

5 files changed

+67
-72
lines changed

5 files changed

+67
-72
lines changed

js/scripts/.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module.exports = {
55
},
66
"extends": "eslint:recommended",
77
"parserOptions": {
8-
"sourceType": "script"
8+
"sourceType": "script",
9+
"ecmaVersion": 2017
910
},
1011
"rules": {
1112
"indent": [

js/scripts/generate-enums.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ function writeJavascriptFile() {
109109
return fse.outputFile(jsEnumDst, content);
110110
}
111111

112-
function createJavascriptFiles() {
113-
return new Promise(function(resolve) {
114-
resolve(writeJavascriptFile());
115-
});
112+
async function createJavascriptFiles() {
113+
return await writeJavascriptFile();
116114
}
117115

118116
function writePythonFile() {
@@ -144,10 +142,8 @@ function writePythonFile() {
144142
return fse.outputFile(pyEnumDst, content);
145143
}
146144

147-
function createPythonFiles() {
148-
return new Promise(function(resolve) {
149-
resolve(writePythonFile());
150-
});
145+
async function createPythonFiles() {
146+
return await writePythonFile();
151147
}
152148

153149
function generateFiles() {

js/scripts/generate-shader-utils.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function mapPromiseFnOverObject(object, mapFn) {
5858
}
5959

6060

61-
function createPythonWrapper(name, relativePath) {
61+
async function createPythonWrapper(name, relativePath) {
6262

6363
const data = THREE[name];
6464
let serialized;
@@ -92,14 +92,14 @@ function createPythonWrapper(name, relativePath) {
9292
generatorScriptName: path.basename(__filename),
9393
});
9494
promises.push(fse.outputFile(pyPath, output));
95-
return Promise.all(promises);
95+
await Promise.all(promises);
9696
}
9797

98-
function createPythonModuleInitFile(modulePath) {
98+
async function createPythonModuleInitFile(modulePath) {
9999

100100
const dirname = path.dirname(modulePath);
101101
const pyInitFilePath = path.resolve(pySrcDir, dirname, '__init__.py');
102-
return fse.ensureFile(pyInitFilePath);
102+
await fse.ensureFile(pyInitFilePath);
103103

104104
}
105105

@@ -110,12 +110,12 @@ function createPythonFiles() {
110110
return Promise.resolve();
111111
}
112112

113-
return mapPromiseFnOverObject(shaderUtilsConfig, function(name, configObj) {
113+
return mapPromiseFnOverObject(shaderUtilsConfig, async function(name, configObj) {
114114
const relativePath = configObj.relativePath;
115-
return createPythonWrapper(name, relativePath).then(function() {
116-
// ensures each dir has empty __init__.py file for proper importing of sub dirs
117-
return createPythonModuleInitFile(relativePath);
118-
});
115+
await createPythonWrapper(name, relativePath);
116+
117+
// ensures each dir has empty __init__.py file for proper importing of sub dirs
118+
await createPythonModuleInitFile(relativePath);
119119
});
120120
}
121121

js/scripts/generate-wrappers.js

+51-53
Original file line numberDiff line numberDiff line change
@@ -601,77 +601,75 @@ function writeJavascriptIndexFiles() {
601601
// Regexp's
602602
const RE_AUTOGEN_EXT = /\.autogen\.js$/;
603603

604-
function writeIndexForDir(dirPath, isTopLevel) {
604+
async function writeIndexForDir(dirPath, isTopLevel) {
605605

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

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

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

616-
// filter excluded files
617-
dirFiles = dirFiles.filter(function(filePath) {
616+
// filter excluded files
617+
dirFiles = dirFiles.filter(filePath => {
618618

619-
// ignore autogen files in _base dir
620-
if (/_base/.test(dirPath) && RE_AUTOGEN_EXT.test(filePath)) {
621-
return false;
622-
}
619+
// ignore autogen files in _base dir
620+
if (/_base/.test(dirPath) && RE_AUTOGEN_EXT.test(filePath)) {
621+
return false;
622+
}
623623

624-
// compare filePath to each exclude pattern
625-
const shouldExclude = _.any(excludes, function(testPattern) {
626-
if (testPattern instanceof RegExp) {
627-
return testPattern.test(filePath);
628-
} else if (typeof testPattern === 'string') {
629-
return testPattern === filePath;
630-
}
631-
});
632-
if (shouldExclude) {
633-
return false;
624+
// compare filePath to each exclude pattern
625+
const shouldExclude = _.any(excludes, function(testPattern) {
626+
if (testPattern instanceof RegExp) {
627+
return testPattern.test(filePath);
628+
} else if (typeof testPattern === 'string') {
629+
return testPattern === filePath;
634630
}
631+
});
632+
if (shouldExclude) {
633+
return false;
634+
}
635635

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

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

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

654-
return true;
655-
});
652+
}
656653

657-
// convert file paths relative to js src dir to paths relative to dirPath
658-
dirFiles = dirFiles.map(function(filePath) {
659-
return './' + path.basename(filePath);
660-
});
654+
return true;
655+
});
661656

662-
// render template
663-
const context = {
664-
now: new Date(),
665-
generatorScriptName: path.basename(__filename),
666-
top_level: isTopLevel,
667-
submodules: dirFiles,
668-
};
669-
const output = jsIndexTemplate(context);
670-
const outputPath = path.resolve(jsSrcDir, dirPath, 'index.js');
657+
// convert file paths relative to js src dir to paths relative to dirPath
658+
dirFiles = dirFiles.map(function(filePath) {
659+
return './' + path.basename(filePath);
660+
});
671661

672-
return fse.outputFile(outputPath, output);
662+
// render template
663+
const context = {
664+
now: new Date(),
665+
generatorScriptName: path.basename(__filename),
666+
top_level: isTopLevel,
667+
submodules: dirFiles,
668+
};
669+
const output = jsIndexTemplate(context);
670+
const outputPath = path.resolve(jsSrcDir, dirPath, 'index.js');
673671

674-
});
672+
return fse.outputFile(outputPath, output);
675673
}
676674

677675
// map over all directories in js src dir

js/scripts/post-build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async function copyExampleImagesToDocs() {
4545
await fse.copy(
4646
path.resolve(exampleImagesSrcDir, filePath),
4747
path.resolve(exampleImagesDstDir, filePath)
48-
)
48+
);
4949
console.log(`Copied ${filePath} to docs examples' image folder`);
5050
}));
5151
}

0 commit comments

Comments
 (0)