Skip to content

Commit

Permalink
Merge pull request #179 from xeho91/chores
Browse files Browse the repository at this point in the history
chore: Remove `fs-extra` in favor of `node:fs`
  • Loading branch information
xeho91 committed May 16, 2024
2 parents 400f257 + fc4c90d commit 69b4c28
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.svelte-kit/
dist/
node_modules/
storybook-static/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"dependencies": {
"@babel/runtime": "^7.22.6",
"dedent": "^1.2.0",
"fs-extra": "^11.1.1",
"magic-string": "^0.30.1"
},
"devDependencies": {
Expand All @@ -69,6 +68,7 @@
"@storybook/test": "^8.0.0-rc.2",
"@storybook/theming": "^8.0.0-rc.2",
"@storybook/types": "^8.0.0-rc.2",
"@sveltejs/kit": "^2.5.7",
"@sveltejs/package": "^2.2.0",
"@sveltejs/vite-plugin-svelte": "^2.4.2",
"@tsconfig/svelte": "^5.0.0",
Expand Down
52 changes: 25 additions & 27 deletions src/config-loader.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// This file is a rewrite of `@sveltejs/vite-plugin-svelte` without the `Vite`
// parts: https://github.com/sveltejs/vite-plugin-svelte/blob/e8e52deef93948da735c4ab69c54aced914926cf/packages/vite-plugin-svelte/src/utils/load-svelte-config.ts
import { fileURLToPath, pathToFileURL } from 'url';
import fs from 'node:fs';
import { createRequire } from 'node:module';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';

import { createRequire } from 'module';
import fs from 'fs';
import { logger } from '@storybook/node-logger';
import path from 'path';
import { pathExists } from 'fs-extra';
import type { Config } from '@sveltejs/kit';

/**
* Try find svelte config and then load it.
*
* @returns {import('@sveltejs/kit').Config | undefined}
* @returns
* Returns the svelte configuration object.
*/
export async function loadSvelteConfig() {
export async function loadSvelteConfig(): Promise<Config | undefined> {
const configFile = await findSvelteConfig();

// no need to throw error since we handle projects without config files
if (configFile === undefined) {
return undefined;
}

let err;
let err: unknown;

// try to use dynamic import for svelte.config.js first
if (configFile.endsWith('.js') || configFile.endsWith('.mjs')) {
Expand Down Expand Up @@ -61,13 +61,13 @@ const importSvelteOptions = (() => {
/**
* Try import specified svelte configuration.
*
* @param {string} configFile
* @param configFile
* Absolute path of the svelte config file to import.
*
* @returns {import('@sveltejs/kit').Config}
* @returns
* Returns the svelte configuration object.
*/
return async (configFile) => {
return async (configFile: string): Promise<Config> => {
const result = await dynamicImportDefault(
pathToFileURL(configFile).href,
fs.statSync(configFile).mtimeMs
Expand All @@ -81,19 +81,18 @@ const importSvelteOptions = (() => {
})();

const requireSvelteOptions = (() => {
/** @type {NodeRequire} */
let esmRequire;
let esmRequire: NodeRequire;

/**
* Try import specified svelte configuration.
*
* @param {string} configFile
* @param configFile
* Absolute path of the svelte config file to require.
*
* @returns {import('@sveltejs/kit').Config}
* @returns
* Returns the svelte configuration object.
*/
return (configFile) => {
return (configFile: string): Config => {
// identify which require function to use (esm and cjs mode)
const requireFn = import.meta.url
? (esmRequire = esmRequire ?? createRequire(import.meta.url))
Expand All @@ -114,10 +113,10 @@ const requireSvelteOptions = (() => {
* Try find svelte config. First in current working dir otherwise try to
* find it by climbing up the directory tree.
*
* @returns {Promise<string | undefined>}
* @returns
* Returns the absolute path of the config file.
*/
async function findSvelteConfig() {
async function findSvelteConfig(): Promise<string | undefined> {
const lookupDir = process.cwd();
let configFiles = await getConfigFiles(lookupDir);

Expand All @@ -140,10 +139,10 @@ async function findSvelteConfig() {
* Returning the first found. Should solves most of monorepos with
* only one config at workspace root.
*
* @returns {Promise<string[]>}
* @returns
* Returns an array containing all available config files.
*/
async function getConfigFilesUp() {
async function getConfigFilesUp(): Promise<string[]> {
const importPath = fileURLToPath(import.meta.url);
const pathChunks = path.dirname(importPath).split(path.sep);

Expand All @@ -164,22 +163,21 @@ async function getConfigFilesUp() {
/**
* Gets all svelte config from a specified `lookupDir`.
*
* @param {string} lookupDir
* @param lookupDir
* Directory in which to look for svelte files.
*
* @returns {Promise<string[]>}
* @returns
* Returns an array containing all available config files.
*/
async function getConfigFiles(lookupDir) {
/** @type {[string, boolean][]} */
const fileChecks = await Promise.all(
async function getConfigFiles(lookupDir: string): Promise<string[]> {
const fileChecks: Array<[string, boolean]> = await Promise.all(
knownConfigFiles.map(async (candidate) => {
const filePath = path.resolve(lookupDir, candidate);
return [filePath, await pathExists(filePath)];
return [filePath, fs.existsSync(filePath)];
})
);

return fileChecks.reduce((files, [file, exists]) => {
return fileChecks.reduce((files: string[], [file, exists]) => {
if (exists) files.push(file);
return files;
}, []);
Expand Down
7 changes: 4 additions & 3 deletions src/preset/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'node:fs/promises';

import { storyNameFromExport, toId } from '@storybook/csf';
import type { IndexInput, IndexedCSFFile, IndexerOptions } from '@storybook/types';
import * as svelte from 'svelte/compiler';

import { extractStories } from '../parser/extract-stories.js';
import fs from 'fs-extra';
import { loadSvelteConfig } from '../config-loader.js';
import { storyNameFromExport, toId } from '@storybook/csf';
import type { IndexInput, IndexedCSFFile, IndexerOptions } from '@storybook/types';

export async function readStories(fileName: string) {
let code = (await fs.readFile(fileName, 'utf-8')).toString();
Expand Down

0 comments on commit 69b4c28

Please sign in to comment.