Skip to content

fix+ref: replace hard-coded name, use path prefix, disambiguate names #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"globals": {
"crypto": true
"crypto": true,
"EventSource": true
},
"module": true,
"browser": true,
Expand Down
60 changes: 32 additions & 28 deletions bin/jswt-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ async function main() {
process.argv.includes("-g") || process.argv.includes("--global");

let pkg = await readPackageJson();
let pkgName = pkg.name;
if (pkgName.includes("/")) {
let mainBasename = pkg.name;
if (mainBasename.includes("/")) {
let index = 1;
index += pkgName.lastIndexOf("/");
pkgName = pkgName.slice(index);
index += mainBasename.lastIndexOf("/");
mainBasename = mainBasename.slice(index);
}
let moduleName = toTitleCase(mainBasename);

// await Fs.mkdir("./docs", { recursive: true });
// let fh = await Fs.open("./docs/.gitkeep", "a");
Expand Down Expand Up @@ -81,9 +82,9 @@ async function main() {
"// auto-generated by `jswt reexport`",
"// DO NOT EDIT",
"",
`import ${pkgName} from "${prefix}/${pkgName}.js";`,
`import ${moduleName} from "${prefix}/${mainBasename}.js";`,
"",
`export default ${pkgName};`,
`export default ${moduleName};`,
"",
].join("\n"),
);
Expand All @@ -95,7 +96,8 @@ async function main() {
jsconfig = await createJsConfig(
pkg,
tsconfigTxt,
`${prefix}/${pkgName}.js`,
prefix,
`${prefix}/${mainBasename}.js`,
);
}

Expand All @@ -107,7 +109,8 @@ async function main() {
[
`{`,
` "globals": {`,
` "crypto": true`,
` "crypto": true,`,
` "EventSource": true`,
` },`,
` "module": true,`,
` "browser": true,`,
Expand Down Expand Up @@ -215,21 +218,22 @@ async function main() {

let indexNames = ["index.js", "./index.js"];
let mainIsIndex = indexNames.includes(pkg.main);
let mainPath = `${prefix}/${pkgName}.js`;
let mainFilePath = `${prefix}/${mainBasename}.js`;
if (!mainIsIndex) {
mainPath = pkg.main;
// ensures that filepath has leading './'
mainFilePath = Path.relative(".", pkg.main);
mainFilePath = `./${mainFilePath}`;
}

{
let mainName = toTitleCase(pkgName);
await initFile(
mainPath,
mainFilePath,
[
`let ${mainName} = {};`,
`let ${moduleName} = {};`,
"",
`${mainName}.answer = 42;`,
`${moduleName}.answer = 42;`,
"",
`export default ${mainName};`,
`export default ${moduleName};`,
"",
].join("\n"),
);
Expand All @@ -238,7 +242,7 @@ async function main() {
let hasIndex = await fileExists("./index.js");
if (!hasIndex) {
if (mainIsIndex) {
let allArgs = ["pkg", "set", `main=${mainPath}`];
let allArgs = ["pkg", "set", `main=${mainFilePath}`];
await exec("npm", allArgs);
await sortAndWritePackageJson();
}
Expand Down Expand Up @@ -284,10 +288,9 @@ async function main() {
let allArgs = [
"pkg",
"set",
`files[]=${mainPath}`,
`files[]=${mainFilePath}`,
`files[]=./bin/*.js`,
`files[]=./lib/*.js`,
`files[]=./tests/*.js`,
];
await exec("npm", allArgs);
await sortAndWritePackageJson();
Expand Down Expand Up @@ -315,8 +318,8 @@ async function main() {
"pkg",
"set",
"type=module",
`exports[.]=${mainPath}`,
`exports[./*]=./*`,
`exports[.]=${mainFilePath}`,
`exports[./*]=${prefix}/*`,
];
await exec("npm", allArgs);
await sortAndWritePackageJson();
Expand All @@ -331,7 +334,8 @@ async function main() {
"pkg",
"set",
"type=module",
`imports[${pkg.name}]=${mainPath}`,
`imports[${pkg.name}]=${mainFilePath}`,
`imports[${pkg.name}/]=${prefix}/`,
];
await exec("npm", allArgs);
await sortAndWritePackageJson();
Expand All @@ -342,9 +346,8 @@ async function main() {
`<script type="importmap">`,
` {`,
` "imports": {`,
` "@root/passkey": "./passkey.js",`,
` "@root/passkey/": "./lib/",`,
` "localstore": "./vendor/localstore.js"`,
` "${pkg.name}": "${mainFilePath}",`,
` "${pkg.name}/": "${prefix}/"`,
` }`,
` }`,
`</script>`,
Expand Down Expand Up @@ -627,10 +630,11 @@ async function getLatest20xx() {
* @param {Object} pkg
* @param {String} pkg.name
* @param {String} tsconfigTxt
* @param {String} mainPath
* @param {String} prefix
* @param {String} mainFilePath
* @returns
*/
async function createJsConfig(pkg, tsconfigTxt, mainPath) {
async function createJsConfig(pkg, tsconfigTxt, prefix, mainFilePath) {
if (!tsconfigTxt.includes(`"include":`)) {
let includables = [
"*.js",
Expand All @@ -649,8 +653,8 @@ async function createJsConfig(pkg, tsconfigTxt, mainPath) {

{
let lines = [
` "${pkg.name}": ["${mainPath}"]`,
` "${pkg.name}/*": ["./*"]`,
` "${pkg.name}": ["${mainFilePath}"]`,
` "${pkg.name}/*": ["${prefix}/*"]`,
];
let str = lines.join(`,\n`);
tsconfigTxt = tsconfigTxt.replace(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jswt",
"version": "2.0.2",
"version": "2.0.3",
"description": "Turn on transpile-free type hinting for your vanilla JS projects #JSWithTypes",
"main": "./jswt.js",
"files": [
Expand Down
Loading