You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WebCAD/utils/copy_type.js

90 lines
2.2 KiB

//拯救type错误的问题
let http = require('https');
let fs = require('fs');
let path = require('path');
function downLoadFile(url, path)
{
if (fs.exists(path)) {
fs.unlink(path);
}
let file = fs.createWriteStream(path);
let request = http.get(url, function (response)
{
response.pipe(file);
});
}
function copyFileSync(source, target)
{
let targetFile = target;
//if target is a directory a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
console.log('targetFile: ', targetFile);
}
function copyFolderRecursiveSync(source, target)
{
let files = [];
//check if folder needs to be created or integrated
let targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file)
{
let curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
let filePath = "./@types/";
let modules_path = "./node_modules/";
copyFolderRecursiveSync(filePath, modules_path);
let downFiles =
{
name: "three",
urlPath: "https://raw.githubusercontent.com/FishOrBear/DefinitelyTyped/master/types/three/",
files: [
// "index.d.ts",
"three-core.d.ts",
// "three-outlinepass.d.ts",
// "three-smaapass.d.ts"
]
}
filePath = path.resolve("./node_modules/@types/" + downFiles.name) + "\\";
console.log('filePath: ', filePath);
for (let file of downFiles.files) {
console.log(downFiles.urlPath + file);
try {
downLoadFile(downFiles.urlPath + file, filePath + file);
} catch (error) {
console.log('error: ', error);
}
}