Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created April 5, 2023 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinokada/c740e5092dd76ad6dbf2e80b02b00462 to your computer and use it in GitHub Desktop.
Save shinokada/c740e5092dd76ad6dbf2e80b02b00462 to your computer and use it in GitHub Desktop.
// 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