Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Created May 13, 2023 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BananaAcid/a3340e7daa8c6698eb7f1940ea5eff4a to your computer and use it in GitHub Desktop.
Save BananaAcid/a3340e7daa8c6698eb7f1940ea5eff4a to your computer and use it in GitHub Desktop.
nuxt fn to create a folder

nuxt.config.ts (config upload path)

export default defineNuxtConfig({
    telemetry: false,

    runtimeConfig: {
        paths: {
            upload: path.join(process.cwd(), '../upload'),  //fs.realpathSync( '../upload') --> will be an error, if it does not exist
            
        }
    },
    // ...
});

/server/utils/folderTools.ts (exports will be loaded in all server/**/*.ts)

export async function checkOrCreateUploadFolder(folderName:string) {
    let err:any = false;

    const UPLOAD_PATH = path.join(useRuntimeConfig().paths.upload, folderName);

    let exists = await fs.realpath(UPLOAD_PATH).catch(_ => null);
    
    if (!exists) {
        fs.mkdir(UPLOAD_PATH, {recursive: true}).catch( e => err = {msg: e.msg});

        exists = await fs.realpath(UPLOAD_PATH).catch(_ => null);
    }
    
    return {err, path: exists};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment