@itrocks/prepare-module
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -30,3 +30,3 @@ { | ||
"types": "./prepare-module.d.ts", | ||
"version": "0.0.9" | ||
"version": "0.0.10" | ||
} |
@@ -7,33 +7,39 @@ #!/usr/bin/env node | ||
const destDir = '.'; | ||
async function getNamespace() { | ||
const name = JSON.parse(await fs_1.promises.readFile('package.json', 'utf8')).name; | ||
return name.startsWith('@') | ||
? name.slice(0, name.indexOf('/')) | ||
: undefined; | ||
} | ||
(async () => { | ||
const namespace = await getNamespace(); | ||
const case1 = new RegExp(`import(.*)from\\s+'../node_modules/${namespace}/`, 'g'); | ||
const case2 = new RegExp(`import(.*)from\\s+'../node_modules/`, 'g'); | ||
const files = await fs_1.promises.readdir(srcDir); | ||
for (const file of files) { | ||
const srcPath = srcDir + '/' + file; | ||
const destPath = destDir + '/' + file; | ||
if (file.endsWith('.js')) { | ||
let content = (await fs_1.promises.readFile(srcPath, 'utf8')); | ||
if (namespace) { | ||
content = content | ||
.replace(case1, "import$1from '../") | ||
.replace(case2, "import$1from '../../"); | ||
const case1 = new RegExp(`../node_modules/${namespace}/`, 'g'); | ||
const case2 = new RegExp(`../node_modules/`, 'g'); | ||
async function getNamespace() { | ||
const name = JSON.parse(await fs_1.promises.readFile('package.json', 'utf8')).name; | ||
return name.startsWith('@') | ||
? name.slice(0, name.indexOf('/')) | ||
: undefined; | ||
} | ||
async function prepareDir(srcDir, destDir) { | ||
for (const file of await fs_1.promises.readdir(srcDir)) { | ||
const srcPath = srcDir + '/' + file; | ||
const destPath = destDir + '/' + file; | ||
if ((file[0] !== '.') && (await fs_1.promises.lstat(srcPath)).isDirectory()) { | ||
try { | ||
await fs_1.promises.access(destPath); | ||
} | ||
catch { | ||
await fs_1.promises.mkdir(destPath); | ||
} | ||
await prepareDir(srcPath, destPath); | ||
continue; | ||
} | ||
else { | ||
content = content | ||
.replace(case2, "import$1from '../"); | ||
if (file.endsWith('.js')) { | ||
let content = (await fs_1.promises.readFile(srcPath, 'utf8')); | ||
content = namespace | ||
? content.replace(case1, "../").replace(case2, "../../") | ||
: content.replace(case2, "../"); | ||
await fs_1.promises.writeFile(destPath, content, 'utf8'); | ||
} | ||
await fs_1.promises.writeFile(destPath, content, 'utf8'); | ||
else if (file.endsWith('.d.ts')) { | ||
await fs_1.promises.copyFile(srcPath, destPath); | ||
} | ||
} | ||
else if (file.endsWith('.d.ts')) { | ||
await fs_1.promises.copyFile(srcPath, destPath); | ||
} | ||
} | ||
await prepareDir(srcDir, destDir); | ||
})().catch(); |
6058
46