Skip to content

Commit

Permalink
refactor: move mkdir to writeoutputfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jan 30, 2022
1 parent 71353db commit c130279
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
6 changes: 3 additions & 3 deletions browser/fs.ts
@@ -1,7 +1,7 @@
import { throwNoFileSystem } from './error';

export const promises = {
readFile: throwNoFileSystem('fs.readFile')
mkdir: throwNoFileSystem('fs.mkdir'),
readFile: throwNoFileSystem('fs.readFile'),
writeFile: throwNoFileSystem('fs.writeFile')
};

export const writeFile = throwNoFileSystem('fs.writeFile');
12 changes: 8 additions & 4 deletions src/rollup/rollup.ts
Expand Up @@ -4,7 +4,7 @@ import Graph from '../Graph';
import type { PluginDriver } from '../utils/PluginDriver';
import { ensureArray } from '../utils/ensureArray';
import { errAlreadyClosed, errCannotEmitFromOptionsHook, error } from '../utils/error';
import { writeFile } from '../utils/fs';
import { promises as fs } from '../utils/fs';
import { normalizeInputOptions } from '../utils/options/normalizeInputOptions';
import { normalizeOutputOptions } from '../utils/options/normalizeOutputOptions';
import type { GenericConfigObject } from '../utils/options/options';
Expand Down Expand Up @@ -259,11 +259,15 @@ function getSortingFileType(file: OutputAsset | OutputChunk): SortingFileType {
return SortingFileType.SECONDARY_CHUNK;
}

function writeOutputFile(
async function writeOutputFile(
outputFile: OutputAsset | OutputChunk,
outputOptions: NormalizedOutputOptions
): Promise<unknown> {
const fileName = resolve(outputOptions.dir || dirname(outputOptions.file!), outputFile.fileName);

// 'recursive: true' does not throw if the folder structure, or parts of it, already exist
await fs.mkdir(dirname(fileName), { recursive: true });

let writeSourceMapPromise: Promise<void> | undefined;
let source: string | Uint8Array;
if (outputFile.type === 'asset') {
Expand All @@ -276,15 +280,15 @@ function writeOutputFile(
url = outputFile.map.toUrl();
} else {
url = `${basename(outputFile.fileName)}.map`;
writeSourceMapPromise = writeFile(`${fileName}.map`, outputFile.map.toString());
writeSourceMapPromise = fs.writeFile(`${fileName}.map`, outputFile.map.toString());
}
if (outputOptions.sourcemap !== 'hidden') {
source += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
}
}

return Promise.all([writeFile(fileName, source), writeSourceMapPromise]);
return Promise.all([fs.writeFile(fileName, source), writeSourceMapPromise]);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/utils/fs.ts
@@ -1,9 +1 @@
import { promises as fs } from 'fs';
import { dirname } from './path';

export * from 'fs';

export async function writeFile(dest: string, data: string | Uint8Array): Promise<void> {
await fs.mkdir(dirname(dest), { recursive: true });
await fs.writeFile(dest, data);
}

0 comments on commit c130279

Please sign in to comment.