Created
April 5, 2023 05:29
-
-
Save shinokada/c740e5092dd76ad6dbf2e80b02b00462 to your computer and use it in GitHub Desktop.
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
// Use this script where you have sub-directories | |
// This script reads the files in the dist directory and creates a new exports object in the package.json file | |
import fs from 'fs'; | |
import path from 'path'; | |
const distDir = './dist'; | |
const packageJsonPath = './package.json'; | |
const componentNames = fs.readdirSync(distDir); | |
const componentExports = {}; | |
for (const componentName of componentNames) { | |
const componentDir = path.join(distDir, componentName); | |
if (!fs.existsSync(componentDir) || !fs.lstatSync(componentDir).isDirectory()) { | |
continue; | |
} | |
const componentFiles = fs.readdirSync(componentDir); | |
const svelteFiles = componentFiles.filter((file) => file.endsWith('.svelte')); | |
for (const svelteFile of svelteFiles) { | |
const dtsFile = `${svelteFile}.d.ts`; | |
const exportKey = `./${svelteFile}`; | |
componentExports[exportKey] = { | |
types: `./dist/${componentName}/${dtsFile}`, | |
svelte: `./dist/${componentName}/${svelteFile}`, | |
}; | |
} | |
} | |
const indexDtsPath = path.join(distDir, 'index.d.ts'); | |
if (fs.existsSync(indexDtsPath) && fs.lstatSync(indexDtsPath).isFile()) { | |
componentExports['.'] = { | |
types: './dist/index.d.ts', | |
svelte: './dist/index.js', | |
}; | |
} | |
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); | |
packageJson.exports = componentExports; | |
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment