Skip to content

Commit

Permalink
chore: read package.json manually as node experimental features can b…
Browse files Browse the repository at this point in the history
…e changed without notice
  • Loading branch information
orefalo committed Jun 18, 2024
1 parent 78bbdf3 commit 276ca6e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 15 deletions.
16 changes: 8 additions & 8 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ export default [
}
},
{
// 1. Target ```js code blocks in .md files.
files: ["**/*.md/*.js"],
rules: {
// 2. Disable other rules.
"no-console": "off",
"import/no-unresolved": "off"
}
},
// 1. Target ```js code blocks in .md files.
files: ['**/*.md/*.js'],
rules: {
// 2. Disable other rules.
'no-console': 'off',
'import/no-unresolved': 'off'
}
},
{
ignores: removeDuplicates([...readGitignoreFiles({ cwd: __dirname }), '**/.svelte-kit/', '**/_app/', 'package/'])
},
Expand Down
2 changes: 0 additions & 2 deletions scripts/create-minified-size-badges.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import fs from 'fs';

import { makeBadge } from 'badge-maker';

/**
*
* @param {string} name
*/
const formatSize = (name) =>
Expand Down
4 changes: 3 additions & 1 deletion scripts/fetch-and-publish.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// issues the npm command to publish the package
import child_process from 'node:child_process';
import os from 'node:os';
import packageJson from '../package.json' assert { type: 'json' };
import { readPackageJSON } from './read-package-json.js';

if (process.argv.length < 3) {
console.error('Usage: fetch-and-publish <NPM_OTP>');
process.exit(1);
}

const packageJson = readPackageJSON();

const otp = process.argv[process.argv.length - 1];
const version = packageJson.version;

Expand Down
5 changes: 3 additions & 2 deletions scripts/package-clean-copy.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { writeFile } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';

import packageJson from '../package.json' assert { type: 'json' };
import { readPackageJSON } from './read-package-json.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const packageJsonOutputPath = resolve(__dirname, '../package/package.json');

const packageJson = readPackageJSON();

// Keys that we want to remove, which are not usefull for the final package that the user consumes.
const keysToRemove = ['private', 'devDependencies', 'optionalDependencies', 'scripts', 'config'];
for (const key of keysToRemove) {
Expand Down
33 changes: 33 additions & 0 deletions scripts/read-package-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs';
import path from 'path';

const __dirname = path.dirname(import.meta.url);

function findPackageJson(currentPath) {
let filePath = path.join(currentPath, 'package.json');

while (!fs.existsSync(filePath) && currentPath !== path.parse(currentPath).root) {
currentPath = path.dirname(currentPath);
filePath = path.join(currentPath, 'package.json');
}

return fs.existsSync(filePath) ? filePath : null;
}

export function readPackageJSON(currentPath) {
const packageJsonPath = currentPath ? findPackageJson(currentPath) : findPackageJson(__dirname);

if (packageJsonPath) {
try {
const data = fs.readFileSync(packageJsonPath, 'utf8');
const packageData = JSON.parse(data);
return packageData;
} catch (err) {
console.error('Error reading package.json:', err);
return null;
}
} else {
console.error('package.json not found');
return null;
}
}
1 change: 0 additions & 1 deletion scripts/vite/example-import.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs/promises';

import * as sass from 'sass';

// This Highlight.js loading is based on the code in:
Expand Down
5 changes: 4 additions & 1 deletion svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import adapter from '@sveltejs/adapter-static';
import { sveltePreprocess } from 'svelte-preprocess';
import path from 'path';
import packageJson from './package.json' assert { type: 'json' };

import { readPackageJSON } from './scripts/read-package-json.js';
const packageJson = readPackageJSON();

// IMPORTANTL if you update aliases, run `pnpm run dev` for the configuration to update (tsconfig.json)
const alias = {
$comp: path.resolve('./src/lib/comp')
};


// alias used by our vite plugin to resolve file, it's the pkg name!
// alias: package name -> src/lib
alias[packageJson.name] = path.resolve('.', 'src/lib');
Expand Down

0 comments on commit 276ca6e

Please sign in to comment.