fs#stat TypeScript Examples

The following examples show how to use fs#stat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: finder.ts    From devoirs with MIT License 6 votes vote down vote up
// noinspection JSMethodCanBeStatic
  private isDirectoryExists(path: string): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
      stat(path, (error: NodeJS.ErrnoException, stats: Stats) => {
        if (error) {
          if (error.code !== 'ENOENT') {
            return reject(error);
          }

          return resolve(false);
        }

        resolve(stats.isDirectory());
      });
    });
  }
Example #2
Source File: index.ts    From write-file-action with Apache License 2.0 5 votes vote down vote up
statAsync = promisify(stat)
Example #3
Source File: extension.ts    From vscode-windows-terminal with MIT License 5 votes vote down vote up
async function isFile(path: string): Promise<boolean> {
  const result = await promisify(stat)(path);
  return !result.isDirectory();
}
Example #4
Source File: duckdb-config.test.ts    From node-duckdb with MIT License 5 votes vote down vote up
statAsync = promisify(stat)
Example #5
Source File: executor.ts    From nx-dotnet with MIT License 5 votes vote down vote up
async function exists(path: string) {
  return new Promise((resolve) => {
    stat(path, (err) => resolve(err ? false : true));
  });
}
Example #6
Source File: local-design-manager.ts    From open-design-sdk with Apache License 2.0 5 votes vote down vote up
statPromised = promisify(stat)
Example #7
Source File: pandoc.ts    From obsidian-pandoc with MIT License 4 votes vote down vote up
pandoc = async (input: PandocInput, output: PandocOutput, extraParams?: string[])
    : Promise<{ result: string, command: string, error: string }> => new Promise(async (resolve, reject) => {
    const stdin = input.file === 'STDIN';
    const stdout = output.file === 'STDOUT';

    let pandoc: ChildProcess;
    let result = '';
    let error = '';

    const fileBaseName = (file: string): string => path.basename(file, path.extname(file));

    // Construct the Pandoc arguments list
    let args: string[] = [];

    if (input.format) {
        args.push('--from');
        args.push(input.format);
    }
    if (output.format) {
        args.push('--to');
        args.push(output.format);
    }
    if (needsStandaloneFlag(output))
        args.push('-s');
    if (!stdout) {
        args.push('-o');
        args.push(output.file);
    } else {
        args.push('-o');
        args.push('-');
    }
    // // Support Unicode in the PDF output if XeLaTeX is installed
    if (output.format === 'pdf' && await lookpath('xelatex'))
        args.push('--pdf-engine=xelatex');
    if (!stdin) {
        args.push(input.file);
    }
    // The metadata title is needed for ePub and standalone HTML formats
    // We use a metadata file to avoid being vulnerable to command injection
    if (input.metadataFile) args.push('--metadata-file', input.metadataFile);
    // Extra parameters
    if (extraParams) {
        extraParams = extraParams.flatMap(x => x.split(' ')).filter(x => x.length);
        args.push(...extraParams);
    }

    function start () {
        // Spawn a Pandoc child process
        // Assumes Pandoc is installed and that the arguments are valid
        // The arguments aren't sanitised, so be careful!
        const env = Object.assign(process.env);

        if (input.pdflatex) {
            // Workaround for Windows having different PATH delimiters
            // to *every other operating system in existence*
            // *sigh*
            if (process.platform === 'win32')
                env.PATH += ";"
            else
                env.PATH += ":";
            env.PATH += path.dirname(input.pdflatex);
        }
        pandoc = spawn(input.pandoc || 'pandoc', args, { env: process.env });

        if (stdin) {
            // TODO: strip some unicode characters but not others
            // Currently we're stripping footnote back arrows but no
            // other characters to avoid localisation issues
            const contents = input.contents.replace(/[\u21a9\ufe0e]/g, '');
            pandoc.stdin.write(contents);
            pandoc.stdin.end();
        }

        // Handlers
        pandoc.stdout.on('data', (data: any) => {
            result += data;
        });
        pandoc.stderr.on('data', (err: any) => {
            error += err;
        });
        pandoc.stdout.on('end', () => {
            const value = {
                result, error,
                command: 'pandoc ' + args.join(' ')
            };
            if (output.file !== 'STDOUT') {
                fs.stat(output.file, (err: NodeJS.ErrnoException | null, stats: fs.Stats) => {
                    // Call resolve if the file exists, reject otherwise
                    if (stats && stats.isFile()) {
                        resolve(value);
                    } else {
                        reject(error);
                    }
                });
            } else {
                // Call resolve iff there is a nonempty result
                (result.length ? resolve : reject)(value);
                if (result.length) {
                    resolve(value);
                } else {
                    reject(error);
                }
            }
        });
    }

    if (input.file === 'STDIN') {
        start();
    } else {
        // Check if the input file exists, and then start
        stat(input.file, (err: NodeJS.ErrnoException | null, stats: Stats) => {
            if (stats.isFile()) start();
            else reject(new Error('Input file does not exist'));
        });
    }
})