Created
July 29, 2022 08:24
-
-
Save clukhei/38f64ffa79f2045b816c0696dc3add67 to your computer and use it in GitHub Desktop.
subpath-import-postbuild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
const { resolve, join, basename } = require('path'); | |
const { readFile, writeFile, copy } = require('fs-extra'); | |
const packagePath = process.cwd(); | |
const distPath = join(packagePath, './dist'); | |
const writeJson = (targetPath, obj) => | |
writeFile(targetPath, JSON.stringify(obj, null, 2), 'utf8'); | |
async function createPackageFile() { | |
const packageData = await readFile( | |
resolve(packagePath, './package.json'), | |
'utf8' | |
); | |
const { scripts, devDependencies, ...packageOthers } = | |
JSON.parse(packageData); | |
const newPackageData = { | |
...packageOthers, | |
private: false, | |
typings: './index.d.ts', | |
main: './cjs/index.js', | |
module: './index.js', | |
}; | |
const targetPath = resolve(distPath, './package.json'); | |
await writeJson(targetPath, newPackageData); | |
console.log(`Created package.json in ${targetPath}`); | |
} | |
async function includeFileInBuild(file) { | |
const sourcePath = resolve(packagePath, file); | |
const targetPath = resolve(distPath, basename(file)); | |
await copy(sourcePath, targetPath); | |
console.log(`Copied ${sourcePath} to ${targetPath}`); | |
} | |
async function run() { | |
try { | |
await createPackageFile(); | |
await includeFileInBuild('./README.md'); | |
// await includeFileInBuild('../../LICENSE'); | |
} catch (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment