diff --git a/build/build.js b/build/build.js index 8222eb9..d2e6d62 100755 --- a/build/build.js +++ b/build/build.js @@ -17,7 +17,7 @@ limitations under the License. */ import { processPlugin } from './utils/plugin.js'; -import { readdir } from 'node:fs/promises'; +import { readdir, rm } from 'node:fs/promises'; import { join } from 'node:path'; import { pathToFileURL } from 'node:url'; import { parseArgs } from 'node:util'; @@ -47,6 +47,11 @@ const { values: args } = parseArgs({ } }); +await rm(args.distBase, { + recursive: true, + force: true +}); + (await Promise.all( files .filter((file) => file.endsWith('.js')) diff --git a/build/utils/artifact.js b/build/utils/artifact.js index 7068bd2..8575fed 100644 --- a/build/utils/artifact.js +++ b/build/utils/artifact.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { checkFileReadAccess, exists, isDirectory } from './fileAccess.js'; +import { exists, isDirectory } from './fileAccess.js'; import { cp, mkdir, writeFile } from 'node:fs/promises'; import { dirname, join } from 'node:path'; @@ -29,27 +29,7 @@ const validateArtifact = function validateArtifact (artifact) { ) ); } else if (artifact.type === 'copy') { - if (typeof artifact.options.source !== 'string') return false; - return checkFileReadAccess(artifact.options.source); - } - return valid; -}; - -const checkPath = async function checkPath (path) { - const directory = dirname(path); - if (await exists(directory) && !await isDirectory(directory)) { - console.warn('Skipping artifact as parent directory exists and is not a directory.'); - return false; - } else { - try { - if (!await exists(directory)) { - await mkdir(directory, { recursive: true }); - } - return true; - } catch (err) { - console.warn('Skipping artifact as directory is not creatable.'); - return false; - } + return typeof artifact.options.source === 'string'; } }; @@ -58,8 +38,11 @@ export const processArtifact = async function processArtifact (artifact, { srcBa console.warn('Skipping artifact as it is invalid.'); return; } - const distPath = join(distBase, artifact.path) - if (!await checkPath(distPath)) return; + const distPath = join(distBase, artifact.path); + const distPathDirectory = dirname(distPath); + if (!await exists(distPathDirectory)) { + await mkdir(distPathDirectory, { recursive: true }); + } if (artifact.type === 'text') { await writeFile( distPath, diff --git a/build/utils/fileAccess.js b/build/utils/fileAccess.js index 9cbb439..9747ecc 100644 --- a/build/utils/fileAccess.js +++ b/build/utils/fileAccess.js @@ -16,16 +16,6 @@ limitations under the License. import { access, constants, stat } from 'node:fs/promises'; -export const checkFileReadAccess = async function checkFileWriteAccess (path) { - try { - await access(path, constants.F_OK | constants.R_OK); - return true; - } catch (err) { - if (err.code === 'ENOENT' || err.code === 'EACCES') return false; - else throw err; - } -}; - export const exists = async function exists (path) { try { await access(path, constants.F_OK);