Last active
March 14, 2023 21:12
-
-
Save shinokada/3b75e69c89471d90c716921f4e37fbbe 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 with the following script | |
``` | |
"write-package-json": "node ./scripts/package-json-writer.js", | |
"add-exports": "node ./scripts/add-exports-to-package-json.js", | |
"package:publish": "standard-version && git push --follow-tags origin main && npm run package && | |
npm run add-exports && npm run write-package-json && npm publish", | |
``` | |
This script reads the content of a folder named 'dist' and generates an exports object to be included in the | |
'package.json' file. The script looks for folders in the 'dist' directory, and for each folder, it checks for | |
any '.svelte' files present. For each '.svelte' file found, it generates an object that includes the path | |
to the '.d.ts' file and the '.svelte' file. Finally, the script adds the generated exports object to the | |
'exports' property of the 'package.json' file. | |
Example results: The exports object added to the 'exports' property of the 'package.json' file would look | |
something like this: | |
"exports": { | |
".": { | |
"types": "./dist/index.d.ts", | |
"svelte": "./dist/index.js" | |
}, | |
"./Component1.svelte": { | |
"types": "./dist/Component1/Component1.svelte.d.ts", | |
"svelte": "./dist/Component1/Component1.svelte" | |
}, | |
"./Component2.svelte": { | |
"types": "./dist/Component2/Component2.svelte.d.ts", | |
"svelte": "./dist/Component2/Component2.svelte" | |
} | |
} | |
*/ | |
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