fs/promises#readFile TypeScript Examples
The following examples show how to use
fs/promises#readFile.
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: cache.ts From cloudmusic-vscode with MIT License | 7 votes |
static async get(key: string): Promise<LyricCacheItem | void> {
try {
const path = resolve(LYRIC_CACHE_DIR, key);
const data = JSON.parse(
(await readFile(path)).toString()
) as LyricCacheItem;
// 7 * 24 * 60 * 60 * 1000
if (Date.now() - data.ctime < 604800000) return data;
void rm(path, { recursive: true, force: true });
} catch {}
return;
}
Example #2
Source File: utils.ts From rewind with MIT License | 7 votes |
export async function determineSongsFolder(osuFolderPath: string, userName: string) {
// First read the username specific config file that has important information such as the
const userConfigPath = join(osuFolderPath, osuUserConfigFileName(userName));
try {
const data = await readFile(userConfigPath, "utf-8");
const records = osuUserConfigParse(data);
const beatmapDirectory = records["BeatmapDirectory"];
return resolve(osuFolderPath, beatmapDirectory);
} catch (err) {
return undefined;
}
}
Example #3
Source File: fetch-api.ts From mtcute with GNU Lesser General Public License v3.0 | 6 votes |
async function updateReadme(currentLayer: number) {
const oldReadme = await readFile(README_MD_FILE, 'utf8')
const today = new Date().toLocaleDateString('ru')
await writeFile(
README_MD_FILE,
oldReadme.replace(
/^Generated from TL layer \*\*\d+\*\* \(last updated on \d+\.\d+\.\d+\)\.$/m,
`Generated from TL layer **${currentLayer}** (last updated on ${today}).`
)
)
}
Example #4
Source File: fetch-api.ts From mtcute with GNU Lesser General Public License v3.0 | 6 votes |
async function overrideInt53(schema: TlFullSchema): Promise<void> {
console.log('Applying int53 overrides...')
const config = JSON.parse(
await readFile(join(__dirname, '../data/int53-overrides.json'), 'utf8')
)
schema.entries.forEach((entry) => {
const overrides: string[] | undefined = config[entry.kind][entry.name]
if (!overrides) return
overrides.forEach((argName) => {
const arg = entry.arguments.find((it) => it.name === argName)
if (!arg) {
console.log(
`[warn] Cannot override ${entry.name}#${argName}: argument does not exist`
)
return
}
if (arg.type === 'long') {
arg.type = 'int53'
} else if (arg.type.toLowerCase() === 'vector<long>') {
arg.type = 'vector<int53>'
} else {
console.log(
`[warn] Cannot override ${entry.name}#${argName}: argument is not long (${arg.type})`
)
}
})
})
}
Example #5
Source File: documentation.ts From mtcute with GNU Lesser General Public License v3.0 | 6 votes |
export async function getCachedDocumentation(): Promise<CachedDocumentation | null> {
try {
const file = await readFile(DOC_CACHE_FILE, 'utf8')
return JSON.parse(file)
} catch (e: any) {
if (e.code === 'ENOENT') {
return null
}
throw e
}
}
Example #6
Source File: gen-code.ts From mtcute with GNU Lesser General Public License v3.0 | 6 votes |
async function main() {
const errors: TlErrors = JSON.parse(
await readFile(ERRORS_JSON_FILE, 'utf8')
)
const [apiSchema, apiLayer] = unpackTlSchema(
JSON.parse(await readFile(API_SCHEMA_JSON_FILE, 'utf8'))
)
const mtpSchema = parseFullTlSchema(
JSON.parse(await readFile(MTP_SCHEMA_JSON_FILE, 'utf8'))
)
await generateTypings(apiSchema, apiLayer, mtpSchema, errors)
await generateReaders(apiSchema, mtpSchema)
await generateWriters(apiSchema, mtpSchema)
console.log('Done!')
}
Example #7
Source File: arweave-bundle.ts From metaplex with Apache License 2.0 | 6 votes |
/**
* Retrieve an asset's manifest from the filesystem & update it with the link
* to the asset's image/animation link, obtained from signing the asset image/animation DataItem.
*/
async function getUpdatedManifest(
manifestPath: string,
imageLink: string,
animationLink: string,
): Promise<Manifest> {
const manifest: Manifest = JSON.parse(
(await readFile(manifestPath)).toString(),
);
const originalImage = manifest.image;
manifest.image = imageLink;
manifest.properties.files.forEach(file => {
if (file.uri === originalImage) file.uri = imageLink;
});
if (animationLink) {
manifest.animation_url = animationLink;
}
return manifest;
}
Example #8
Source File: bundles.test.ts From iuliia-js with MIT License | 6 votes |
describe("Webpack Tree-Shaking", () => {
it("should create a tree-shaked bundle with only necesssary schemas included", async () => {
const bundle = join(webpackConfig.output.path, webpackConfig.output.filename as string);
await new Promise((res, rej) =>
webpack(webpackConfig, (err, stats) => (err ? rej(err) : res(stats)))
);
const { size } = await stat(bundle);
expect(size).toBeLessThan(5_700);
expect(size).toBeGreaterThan(5_000);
const contents = await readFile(bundle, "utf8");
const matches = contents.match(/https:\/\/iuliia.ru\/([^/]+)/gim);
expect(matches).toEqual(["https://iuliia.ru/mosmetro"]);
const consoleLog = jest.fn();
runInContext(
await readFile(bundle, "utf8"),
createContext({ console: { log: consoleLog } })
);
expect(consoleLog).toBeCalledWith("Privet, mir!");
});
});
Example #9
Source File: generate.ts From iuliia-js with MIT License | 6 votes |
async function jsonToTS(inputFile: string, v: ValidateFunction): Promise<string> {
const input = await readFile(inputFile, "utf8");
const data = JSON.parse(input);
if (!v(data)) {
console.error(`Validation failed for file ${inputFile}:`);
console.error(v.errors);
process.exit(1);
}
return (
"import {TransliterationSchema} from './TransliterationSchema';\n\n" +
`export default ${JSON.stringify(data)} as TransliterationSchema;`
);
}
Example #10
Source File: [second].tsx From core with GNU Affero General Public License v3.0 | 6 votes |
export async function getStaticProps () {
const docs = await Promise.all((await readdir(docsDir)).map(async el => {
const isDir = (await lstat(join(docsDir, el))).isDirectory()
if(!isDir) {
return {
name: el.split('.').slice(0, -1).join('.'),
text: (await readFile(join(docsDir, el))).toString()
}
}
else {
return {
name: el,
list: await Promise.all((await readdir(join(docsDir, el))).map(async e => {
return {
name: e.split('.').slice(0, -1).join('.'),
text: (await readFile(join(docsDir, el, e))).toString()
}
}))
}
}
}))
return {
props: { docs }
}
}
Example #11
Source File: identity.ts From hoprnet with GNU General Public License v3.0 | 6 votes |
/**
* Tries to read the identity file from disk
* @param path file system path to identity file
* @returns Either identity or undefined
*/
async function loadIdentity(path: string): Promise<Uint8Array | undefined> {
let identity: Uint8Array
try {
identity = Uint8Array.from(await readFile(resolve(path)))
} catch (err) {
log(`Could not load identity file ${path}`)
return
}
return identity
}
Example #12
Source File: bitcoin_rpc.ts From sapio-studio with Mozilla Public License 2.0 | 6 votes |
async function load_node_from_prefs(): Promise<Client> {
await preferences.initialize();
let network = preferences.data.bitcoin.network.toLowerCase();
if (network === 'bitcoin') network = 'mainnet';
const port = preferences.data.bitcoin.port;
const host = preferences.data.bitcoin.host;
let split: string[];
if ('CookieFile' in preferences.data.bitcoin.auth) {
const cookie = preferences.data.bitcoin.auth.CookieFile;
const upw = await readFile(cookie, { encoding: 'utf-8' });
split = upw.split(':');
} else if ('UserPass' in preferences.data.bitcoin.auth) {
split = preferences.data.bitcoin.auth.UserPass;
} else {
console.log('BROKEN');
const client = new Client({
network,
port,
host,
});
return client;
}
if (split.length === 2) {
const username = split[0] ?? '';
const password = split[1] ?? '';
const client = new Client({
network,
username,
password,
port,
host,
});
return client;
} else {
throw Error('Malformed Cookie File');
}
}
Example #13
Source File: lockfile.test.ts From tesseract-server with MIT License | 6 votes |
describe('yarn.lock', () => {
it('should only have resolved urls from registry.npmjs.org', async () => {
const lockfile = await readFile(join(__dirname, '../yarn.lock'), {
encoding: 'utf8',
});
const matches = Array.from(lockfile.matchAll(RESOLVED_REGEX));
matches
.map(match => match[1]?.trim())
.filter(url => url?.trim())
.map(url => parse(url))
.forEach(url => {
expect(url.hostname).toBe('registry.npmjs.org');
});
});
});
Example #14
Source File: sapio.ts From sapio-studio with Mozilla Public License 2.0 | 6 votes |
async read_bound_data_for(file_name: string) {
const file = this.contract_output_path_name(file_name);
const data = JSON.parse(
await readFile(path.join(file, 'bound.json'), {
encoding: 'utf-8',
})
);
return data;
}
Example #15
Source File: sapio.ts From sapio-studio with Mozilla Public License 2.0 | 6 votes |
async read_args_for(file_name: string) {
const file = this.contract_output_path_name(file_name);
const args = JSON.parse(
await readFile(path.join(file, 'args.json'), {
encoding: 'utf-8',
})
);
return args;
}
Example #16
Source File: difficulty.spec.ts From rewind with MIT License | 6 votes |
test("Performance of ojsama", async () => {
const data = await readFile(TEST_MAPS.SUN_MOON_STAR, "utf-8");
const p = new parser();
p.feed(data);
const map = p.map;
const mods = 0;
const d = new std_diff().calc({ map, mods });
console.log(d.objects.map((d) => d.strains[0] + d.strains[1]));
// Around 150ms: pretty fast for 6000 hitobjects
});
Example #17
Source File: Blueprint.spec.ts From rewind with MIT License | 6 votes |
test("md5hash", async () => {
// PS E:\osu!\Songs\351280 HoneyWorks - Akatsuki Zukuyo> CertUtil -hashfile '.\HoneyWorks - Akatsuki Zukuyo ([C u r
// i]) [Taeyang''s Extra].osu' MD5 MD5 hash of .\HoneyWorks - Akatsuki Zukuyo ([C u r i]) [Taeyang's Extra].osu:
// 535c6e5b4febb48629cbdd4e3a268624 CertUtil: -hashfile command completed successfully.
const expectedHash = "535c6e5b4febb48629cbdd4e3a268624";
const file =
"E:\\osu!\\Songs\\351280 HoneyWorks - Akatsuki Zukuyo\\HoneyWorks - Akatsuki Zukuyo ([C u r i]) [Taeyang's Extra].osu";
const data = await readFile(file);
const hash = createHash("md5");
hash.update(data);
const actualHash = hash.digest("hex");
expect(actualHash).toEqual(expectedHash);
});
Example #18
Source File: privacy.ts From your_spotify with GNU General Public License v3.0 | 6 votes |
initWithFiles = async (filePaths: string[]) => {
const files = await Promise.all(filePaths.map((f) => readFile(f)));
const filesContent = files.map((f) => JSON.parse(f.toString()));
const totalContent = filesContent.reduce<PrivacyItem[]>((acc, curr) => {
acc.push(...curr);
return acc;
}, []);
if (!this.initWithJSONContent(totalContent)) {
return false;
}
return true;
};
Example #19
Source File: full_privacy.ts From your_spotify with GNU General Public License v3.0 | 6 votes |
initWithFiles = async (filePaths: string[]) => {
const files = await Promise.all(filePaths.map((f) => readFile(f)));
const filesContent = files.map((f) => JSON.parse(f.toString()));
const totalContent = filesContent.reduce<FullPrivacyItem[]>((acc, curr) => {
acc.push(...curr);
return acc;
}, []);
if (!this.initWithJSONContent(totalContent)) {
return false;
}
return true;
};
Example #20
Source File: getOpenAPISpec.ts From gengen with MIT License | 6 votes |
export async function getOpenAPISpec(options: IOptions): Promise<IOpenAPI3> {
if (options.file) {
const result = await readFile(options.file, 'utf8');
return JSON.parse(result) as IOpenAPI3;
}
if (options.url) {
const result = await fetch(options.url).then((z) => z.json());
return result as IOpenAPI3;
}
throw new Error('Specify file or url');
}
Example #21
Source File: cache.ts From cloudmusic-vscode with MIT License | 6 votes |
static async init(): Promise<void> {
const names = new Set(
(await readdir(MUSIC_CACHE_DIR, { withFileTypes: true }))
.filter((i) => i.isFile())
.map(({ name }) => name)
);
try {
const list = JSON.parse(
(await readFile(this._listPath)).toString()
) as readonly MusicCacheNode[];
list
.filter(({ name }) => names.has(name))
.reverse()
.forEach((value) => {
names.delete(value.name);
this._addNode(value);
});
} catch {}
void this.store();
for (const name of names) {
const path = resolve(MUSIC_CACHE_DIR, name);
rm(path, { recursive: true, force: true }).catch(logError);
}
}
Example #22
Source File: generate.ts From iuliia-js with MIT License | 5 votes |
async function createValidator() {
const ajv = new Ajv();
return ajv.compile(JSON.parse(await readFile(JSON_SCHEMA_FILE, "utf8")));
}
Example #23
Source File: sapio.ts From sapio-studio with Mozilla Public License 2.0 | 5 votes |
async read_module_for(file_name: string): Promise<string> {
const file = this.contract_output_path_name(file_name);
const mod = await readFile(path.join(file, 'module.json'), {
encoding: 'utf-8',
});
const name = JSON.parse(mod).module;
return name;
}
Example #24
Source File: settings.ts From sapio-studio with Mozilla Public License 2.0 | 5 votes |
preferences: {
data: Data;
save: (which: Prefs, data: object) => Promise<boolean>;
load_preferences: (which: Prefs) => Promise<void>;
initialize: () => Promise<void>;
} = {
data: fill_in_default(),
initialize: async () => {
for (const key of pref_array)
try {
await preferences.load_preferences(key);
} catch {}
},
save: async (which: Prefs, data: object) => {
const conf = path.resolve(
app.getPath('userData'),
which + '_preferences.json'
);
switch (which) {
case 'bitcoin':
case 'display':
case 'local_oracle':
case 'sapio_cli':
await writeFile(conf, JSON.stringify(data));
await preferences.load_preferences(which);
await custom_sapio_config();
break;
default:
return Promise.reject('Bad Request');
}
switch (which) {
case 'bitcoin':
deinit_bitcoin_node();
get_bitcoin_node();
break;
case 'local_oracle':
kill_emulator();
start_sapio_oracle();
break;
}
return true;
},
load_preferences: async (which: Prefs) => {
const conf = path.resolve(
app.getPath('userData'),
which + '_preferences.json'
);
preferences.data[which] = JSON.parse(
await readFile(conf, { encoding: 'utf-8' })
);
},
}
Example #25
Source File: bundles.test.ts From iuliia-js with MIT License | 5 votes |
describe("UMD", () => {
it("should load and execute an UMD bundle", async () => {
const library = await readFile(join(__dirname, "../dist/umd/iuliia.js"), "utf8");
const invocation = 'iuliia.translate("Привет, мир!", iuliia.WIKIPEDIA);';
const res = runInNewContext(`${library}\n${invocation}`);
expect(res).toEqual("Privet, mir!");
});
});
Example #26
Source File: formgen.ts From UsTaxes with GNU Affero General Public License v3.0 | 5 votes |
loadFile = async (path: string): Promise<PDFDocument> => {
const file = await readFile(path)
const bytearray = file.slice(0, file.byteLength)
return await PDFDocument.load(bytearray)
}
Example #27
Source File: fetch-api.ts From mtcute with GNU Lesser General Public License v3.0 | 5 votes |
async function updatePackageVersion(
rl: readline.Interface,
currentLayer: number
) {
const packageJson = JSON.parse(await readFile(PACKAGE_JSON_FILE, 'utf8'))
const version: string = packageJson.version
let [major, minor] = version.split('.').map((i) => parseInt(i))
if (major === currentLayer) {
console.log('Current version: %s. Bump minor version?', version)
const res = await input(rl, '[Y/n] > ')
if (res.trim().toLowerCase() === 'n') {
return
}
} else {
major = currentLayer
minor = 0
}
console.log('Updating package version...')
const versionStr = `${major}.${minor}.0`
packageJson.version = versionStr
await writeFile(PACKAGE_JSON_FILE, JSON.stringify(packageJson, null, 4))
console.log('Updating dependant packages...')
for (const dir of await readdir(PACKAGES_DIR, { withFileTypes: true })) {
if (!dir.isDirectory()) continue
const pkgFile = join(PACKAGES_DIR, dir.name, 'package.json')
let pkg
try {
pkg = JSON.parse(await readFile(pkgFile, 'utf8'))
} catch (e: any) {
if (e.code === 'ENOENT') continue
throw e
}
if (pkg.dependencies && '@mtcute/tl' in pkg.dependencies) {
pkg.dependencies['@mtcute/tl'] = 'workspace:' + versionStr
}
if (pkg.devDependencies && '@mtcute/tl' in pkg.devDependencies) {
pkg.devDependencies['@mtcute/tl'] = 'workspace:' + versionStr
}
await writeFile(pkgFile, JSON.stringify(pkg, null, 4) + '\n')
}
// because i am fucking dumb and have adhd and always forget it lol
console.log(
'Done! Please make sure packages compile before committing and pushing'
)
}
Example #28
Source File: documentation.ts From mtcute with GNU Lesser General Public License v3.0 | 5 votes |
async function main() {
let cached = await getCachedDocumentation()
if (cached) {
console.log('Cached documentation: %d', cached.updated)
}
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
})
const input = (q: string): Promise<string> =>
new Promise((res) => rl.question(q, res))
while (true) {
console.log('Choose action:')
console.log('0. Exit')
console.log('1. Update documentation')
console.log('2. Apply descriptions.yaml')
console.log('3. Apply documentation to schema')
const act = parseInt(await input('[0-3] > '))
if (isNaN(act) || act < 0 || act > 3) {
console.log('Invalid action')
continue
}
if (act === 0) return
if (act === 1) {
const [schema, layer] = unpackTlSchema(
JSON.parse(await readFile(API_SCHEMA_JSON_FILE, 'utf8'))
)
cached = await fetchDocumentation(schema, layer)
}
if (act === 2) {
if (!cached) {
console.log('No schema available, fetch it first')
continue
}
const descriptionsYaml = jsYaml.load(
await readFile(DESCRIPTIONS_YAML_FILE, 'utf8')
)
applyDescriptionsYamlFile(cached, descriptionsYaml)
await writeFile(DOC_CACHE_FILE, JSON.stringify(cached))
}
if (act === 3) {
if (!cached) {
console.log('No schema available, fetch it first')
continue
}
const [schema, layer] = unpackTlSchema(
JSON.parse(await readFile(API_SCHEMA_JSON_FILE, 'utf8'))
)
applyDocumentation(schema, cached)
await writeFile(
API_SCHEMA_JSON_FILE,
JSON.stringify(packTlSchema(schema, layer))
)
}
}
}
Example #29
Source File: faucet.ts From hoprnet with GNU General Public License v3.0 | 5 votes |
/**
* Reads the identity files from the given directory, decrypts
* them and returns their Ethereum addresses
* @param directory directory to look for identity files
* @param password password to decrypt identity files
* @param prefix only take identities with given prefix
* @returns the identities' Ethereum addresses
*/
async function getIdentities(directory: string, password: string, prefix?: string): Promise<string[]> {
let fileNames: string[]
try {
fileNames = await readdir(directory)
} catch (err) {
console.log(err)
return []
}
const identityFiles: string[] = []
for (const fileName of fileNames) {
if ((prefix == null || fileName.startsWith(prefix)) && fileName.endsWith('.id')) {
identityFiles.push(fileName)
}
}
console.log(`identityFiles`, identityFiles)
const identites: string[] = []
for (const identityFile of identityFiles) {
let file: Uint8Array
const path = join(directory, identityFile)
try {
file = await readFile(path)
} catch (err) {
console.log(`Could not access ${path}.`, err)
continue
}
const decoded = await deserializeKeyPair(file, password, true)
if (decoded.success) {
identites.push(PublicKey.fromPeerId(decoded.identity).toAddress().toHex())
} else {
console.log(`Could not decrypt private key from file ${file} using "${password}" as password (without ")`)
}
}
return identites
}