fix(node-utils): Convert writeFile function prototype.

This commit is contained in:
Gervwyk 2022-01-21 11:18:23 +02:00
parent 762df5ffc5
commit 5371430712
9 changed files with 21 additions and 37 deletions

View File

@ -49,10 +49,7 @@ async function updateServerPackageJson({ components, context }) {
// be watching the file to trigger reinstalls
if (newPackageJsonContent !== packageJsonContent) {
context.logger.warn('Plugin dependencies have changed. Updating "package.json".');
await writeFile({
filePath,
content: newPackageJsonContent,
});
await writeFile(filePath, newPackageJsonContent);
}
}

View File

@ -120,10 +120,10 @@ async function generateDefaultTypes() {
})
);
await writeFile({
filePath: path.resolve(process.cwd(), './dist/defaultTypes.json'),
content: JSON.stringify(defaultTypes, null, 2),
});
await writeFile(
path.resolve(process.cwd(), './dist/defaultTypes.json'),
JSON.stringify(defaultTypes, null, 2)
);
}
generateDefaultTypes();

View File

@ -19,7 +19,7 @@ import { writeFile } from '@lowdefy/node-utils';
function createWriteBuildArtifact({ directories }) {
async function writeBuildArtifact({ filePath, content }) {
return writeFile({ filePath: path.resolve(directories.build, filePath), content });
return writeFile(path.resolve(directories.build, filePath), content);
}
return writeBuildArtifact;
}

View File

@ -27,16 +27,13 @@ async function init({ context }) {
throw new Error('Cannot initialize a Lowdefy project, a "lowdefy.yaml" file already exists');
}
context.print.log(`Initializing Lowdefy project`);
await writeFile({
filePath: lowdefyFilePath,
content: lowdefyFile({ version: context.cliVersion }),
});
await writeFile(lowdefyFilePath, lowdefyFile({ version: context.cliVersion }));
context.print.log(`Created 'lowdefy.yaml'.`);
await writeFile({
filePath: path.resolve('./.gitignore'),
content: `.lowdefy/**
.env`,
});
await writeFile(
path.resolve('./.gitignore'),
`.lowdefy/**
.env`
);
context.print.log(`Created '.gitignore'.`);
await context.sendTelemetry();
context.print.succeed(`Project initialized.`);

View File

@ -23,7 +23,7 @@ async function getCliJson({ baseDirectory }) {
const cliJson = await readFile(filePath);
if (!cliJson) {
const appId = uuid();
await writeFile({ filePath, content: JSON.stringify({ appId }, null, 2) });
await writeFile(filePath, JSON.stringify({ appId }, null, 2));
return { appId };
}
return JSON.parse(cliJson);

View File

@ -45,12 +45,10 @@ test('getCliJson, no file exists', async () => {
expect(res).toEqual({ appId: 'uuidv4' });
expect(writeFile.mock.calls).toEqual([
[
{
content: `{
path.resolve(process.cwd(), '.lowdefy/cli.json'),
`{
"appId": "uuidv4"
}`,
filePath: path.resolve(process.cwd(), '.lowdefy/cli.json'),
},
],
]);
});

View File

@ -19,7 +19,7 @@ import { writeFile } from '@lowdefy/node-utils';
function reloadClients({ directories }) {
return async () => {
await writeFile({ filePath: path.join(directories.build, 'reload'), content: `${Date.now()}` });
await writeFile(path.join(directories.build, 'reload'), `${Date.now()}`);
};
}

View File

@ -22,7 +22,7 @@ import { type } from '@lowdefy/helpers';
const mkdirPromise = promisify(fs.mkdir);
const writeFilePromise = promisify(fs.writeFile);
async function writeFile({ filePath, content }) {
async function writeFile(filePath, content) {
if (!type.isString(filePath)) {
throw new Error(
`Could not write file, file path should be a string, received ${JSON.stringify(filePath)}.`

View File

@ -29,10 +29,7 @@ test('writeFile', async () => {
//pass
}
expect(fs.existsSync(filePath)).toBe(false);
await writeFile({
filePath,
content: `Test Write File`,
});
await writeFile(filePath, `Test Write File`);
const res = fs.readFileSync(filePath, 'utf8');
expect(res).toEqual(`Test Write File`);
try {
@ -49,25 +46,20 @@ test('writeFile should create directories if they do not exist', async () => {
await new Promise((resolve) => fsExtra.emptyDir(testBaseDir, resolve));
expect(fs.existsSync(filePath)).toBe(false);
await writeFile({
filePath,
content: `Test Write File`,
});
await writeFile(filePath, `Test Write File`);
const res = fs.readFileSync(filePath, 'utf8');
expect(res).toEqual(`Test Write File`);
await new Promise((resolve) => fsExtra.emptyDir(testBaseDir, resolve));
});
test('readFile error id filepath is not a string', async () => {
await expect(writeFile({ filePath: true, content: `Test Write File` })).rejects.toThrow(
await expect(writeFile(true, `Test Write File`)).rejects.toThrow(
'Could not write file, file path should be a string, received true.'
);
});
test('readFile errors if content is not a string.', async () => {
await expect(
writeFile({ filePath: './test/writeFile/writeFile.txt', content: 123 })
).rejects.toThrow(
await expect(writeFile('./test/writeFile/writeFile.txt', 123)).rejects.toThrow(
'The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received type number (123)'
);
});