fs#readFile JavaScript Examples
The following examples show how to use
fs#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: Resource.js From inkpaint with MIT License | 6 votes |
_loadLocalFile() {
readFile(this.url, (err, data) => {
if (err) {
this._onError(err);
} else {
const body = JSON.parse(data);
this._onXhrLoad(body);
}
});
}
Example #2
Source File: parsers.js From Kadena-Mining-Stratum with GNU General Public License v2.0 | 5 votes |
/**
* This function reads file contents and caches them in a global LRU cache.
* Returns a Promise filepath => content array for all files that we were able to read.
*
* @param filenames Array of filepaths to read content from.
*/
function readSourceFiles(filenames) {
// we're relying on filenames being de-duped already
if (filenames.length === 0) {
return SyncPromise.resolve({});
}
return new SyncPromise(function (resolve) {
var sourceFiles = {};
var count = 0;
var _loop_1 = function (i) {
var filename = filenames[i];
var cache = FILE_CONTENT_CACHE.get(filename);
// We have a cache hit
if (cache !== undefined) {
// If it's not null (which means we found a file and have a content)
// we set the content and return it later.
if (cache !== null) {
sourceFiles[filename] = cache;
}
// eslint-disable-next-line no-plusplus
count++;
// In any case we want to skip here then since we have a content already or we couldn't
// read the file and don't want to try again.
if (count === filenames.length) {
resolve(sourceFiles);
}
return "continue";
}
readFile(filename, function (err, data) {
var content = err ? null : data.toString();
sourceFiles[filename] = content;
// We always want to set the cache, even to null which means there was an error reading the file.
// We do not want to try to read the file again.
FILE_CONTENT_CACHE.set(filename, content);
// eslint-disable-next-line no-plusplus
count++;
if (count === filenames.length) {
resolve(sourceFiles);
}
});
};
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (var i = 0; i < filenames.length; i++) {
_loop_1(i);
}
});
}
Example #3
Source File: js.js From ucompress with ISC License | 5 votes |
minify = (source, {noMinify, sourceMap}) => new Promise((res, rej) => {
readFile(source, (err, data) => {
if (err)
rej(err);
else {
const original = data.toString();
/* istanbul ignore if */
if (noMinify)
res({original, code: original, map: ''});
else {
try {
const mini = sourceMap ?
// TODO: find a way to integrate literals minification
{code: original} :
minifyHTMLLiterals(original, {minifyOptions});
/* istanbul ignore next */
const js = mini ? mini.code : original;
const module = /\.mjs$/.test(source) ||
/\b(?:import|export)\b/.test(js);
terserMinify(
js,
sourceMap ?
{
...terserArgs,
module,
sourceMap: {
filename: source,
url: `${source}.map`
}
} :
{
...terserArgs,
module
}
)
.then(({code, map}) => {
res({original, code, map: sourceMap ? map : ''});
})
.catch(rej);
}
catch (error) {
/* istanbul ignore next */
rej(error);
}
}
}
});
})
Example #4
Source File: ulysses.test.js From emgoto.com with MIT License | 5 votes |
// Keeps failing with EAGAIN: resource temporarily unavailable, read
describe.skip('ulysses script', () => {
mock({
[`scripts/${slug}.textbundle`]: {
assets: {
'image.png': '',
'image-2.png': '',
'image.gif': '',
'image-2.gif': '',
},
'info.json': '',
'text.md': mockInput,
},
posts: {},
static: {},
});
afterAll(() => {
mock.restore();
});
test('should successfully move and rename files', async () => {
await ulysses();
const staticFiles = glob.sync(
join(process.cwd(), 'static', '**'),
);
const expectedStaticFiles = [
'',
slug,
`${slug}/image-2.gif`,
`${slug}/image.gif`,
].map((file) => join(process.cwd(), 'static', file));
expect(staticFiles).toEqual(expectedStaticFiles);
const postFiles = glob.sync(
join(process.cwd(), 'posts', '**'),
);
const expectedPostFiles = [
'',
slug,
`${slug}/image-2.png`,
`${slug}/image.png`,
`${slug}/index.mdx`,
].map((file) => join(process.cwd(), 'posts', file));
expect(postFiles).toEqual(expectedPostFiles);
await new Promise((resolve) => {
const file = join(
process.cwd(),
'posts',
slug,
'./index.mdx',
);
readFile(file, 'utf8', (err, result) => {
expect(result).toEqual(expectedResult);
resolve();
});
});
});
});