mirror of
https://github.com/lowdefy/lowdefy.git
synced 2025-02-17 14:30:34 +08:00
feat(helpers): move file utilities to helpers
This commit is contained in:
parent
d905401eb4
commit
1159ac71e7
4
.pnp.js
generated
4
.pnp.js
generated
@ -3679,7 +3679,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
|
||||
["jest", "npm:26.5.3"],
|
||||
["js-yaml", "npm:3.14.0"],
|
||||
["json5", "npm:2.1.3"],
|
||||
["rimraf", "npm:3.0.2"],
|
||||
["uuid", "npm:8.3.1"],
|
||||
["webpack", "virtual:aad21d373d6721af7549b2798c35c4ea25d83c122770abeaa0a4343ba20d55700fd9e3c5fa3289a7e6f591d09e8790df14d525c719ca4729b178b8644cca082a#npm:5.1.3"],
|
||||
["webpack-cli", "virtual:aad21d373d6721af7549b2798c35c4ea25d83c122770abeaa0a4343ba20d55700fd9e3c5fa3289a7e6f591d09e8790df14d525c719ca4729b178b8644cca082a#npm:4.0.0"]
|
||||
@ -3798,7 +3797,8 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
|
||||
["jest", "npm:26.5.3"],
|
||||
["jest-diff", "npm:26.5.2"],
|
||||
["lodash.merge", "npm:4.6.2"],
|
||||
["query-string", "npm:6.13.6"]
|
||||
["query-string", "npm:6.13.6"],
|
||||
["rimraf", "npm:3.0.2"]
|
||||
],
|
||||
"linkType": "SOFT",
|
||||
}]
|
||||
|
@ -35,7 +35,6 @@
|
||||
"dataloader": "2.0.0",
|
||||
"js-yaml": "3.14.0",
|
||||
"json5": "2.1.3",
|
||||
"rimraf": "3.0.2",
|
||||
"uuid": "8.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
@ -14,14 +14,12 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { get, type } from '@lowdefy/helpers';
|
||||
import { get, getFileExtension, getFileSubExtension, type } from '@lowdefy/helpers';
|
||||
import { nunjucksFunction } from '@lowdefy/nunjucks';
|
||||
import JSON5 from 'json5';
|
||||
import YAML from 'js-yaml';
|
||||
import { v1 as uuid } from 'uuid';
|
||||
|
||||
import getFileExtension, { getFileSubExtension } from '../utils/files/getFileExtension';
|
||||
|
||||
function getRefPath(refDefinition) {
|
||||
if (type.isObject(refDefinition) && refDefinition.path) {
|
||||
return refDefinition.path;
|
||||
|
@ -14,7 +14,7 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import cleanDirectory from '../utils/files/cleanDirectory';
|
||||
import { cleanDirectory } from '@lowdefy/helpers';
|
||||
|
||||
async function cleanOutputDirectory({ context }) {
|
||||
return cleanDirectory(context.outputDirectory);
|
||||
|
@ -14,11 +14,13 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { cleanDirectory } from '@lowdefy/helpers';
|
||||
import cleanOutputDirectory from './cleanOutputDirectory';
|
||||
import cleanDirectory from '../utils/files/cleanDirectory';
|
||||
|
||||
jest.mock('../utils/files/cleanDirectory', () => {
|
||||
return jest.fn();
|
||||
jest.mock('@lowdefy/helpers', () => {
|
||||
return {
|
||||
cleanDirectory: jest.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import writeFile from '../utils/files/writeFile';
|
||||
import { writeFile } from '@lowdefy/helpers';
|
||||
|
||||
class FileSetter {
|
||||
constructor({ baseDirectory }) {
|
||||
|
@ -16,9 +16,7 @@
|
||||
|
||||
import JSON5 from 'json5';
|
||||
import YAML from 'js-yaml';
|
||||
import { type } from '@lowdefy/helpers';
|
||||
import readFile from './readFile';
|
||||
import getFileExtension from './getFileExtension';
|
||||
import { getFileExtension, readFile, type } from '@lowdefy/helpers';
|
||||
|
||||
async function getJsonFile(filePath) {
|
||||
const file = await readFile(filePath);
|
||||
|
@ -14,6 +14,13 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
const defaultMetaLocations = {};
|
||||
const defaultMetaLocations = {
|
||||
Button: {
|
||||
url: 'https://unpkg.com/@lowdefy/blocks-antd@^1.0.0-experimental.1/dist/meta/Button.json',
|
||||
},
|
||||
Context: {
|
||||
url: 'https://unpkg.com/@lowdefy/blocks-antd@^1.0.0-experimental.1/dist/meta/Button.json',
|
||||
},
|
||||
};
|
||||
|
||||
export default defaultMetaLocations;
|
||||
|
@ -15,8 +15,8 @@
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { readFile } from '@lowdefy/helpers';
|
||||
import createCacheKey from './createCacheKey';
|
||||
import readFile from '../files/readFile';
|
||||
|
||||
function createFetchMetaCache({ cacheDirectory }) {
|
||||
async function fetchMetaCache(location) {
|
||||
|
@ -15,8 +15,8 @@
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import { writeFile } from '@lowdefy/helpers';
|
||||
import createCacheKey from './createCacheKey';
|
||||
import writeFile from '../files/writeFile';
|
||||
|
||||
function createWriteMetaCache({ cacheDirectory }) {
|
||||
async function writeMetaCache({ location, meta }) {
|
||||
|
@ -20,7 +20,7 @@ import readJsonFile from './readJsonFile';
|
||||
|
||||
function createPageBatchLoader({ CONFIGURATION_BASE_PATH }) {
|
||||
async function readPage(id) {
|
||||
const filePath = path.resolve(CONFIGURATION_BASE_PATH, `pages/${id}.json`);
|
||||
const filePath = path.resolve(CONFIGURATION_BASE_PATH, `pages/${id}/${id}.json`);
|
||||
return readJsonFile({ filePath });
|
||||
}
|
||||
async function loader(keys) {
|
||||
|
@ -14,19 +14,11 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import { promisify } from 'util';
|
||||
import { readFile } from '@lowdefy/helpers';
|
||||
import { ConfigurationError } from '../context/errors';
|
||||
|
||||
const readFile = promisify(fs.readFile);
|
||||
|
||||
async function readJsonFile({ filePath }) {
|
||||
let file;
|
||||
try {
|
||||
file = await readFile(filePath);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
const file = await readFile(filePath);
|
||||
try {
|
||||
return JSON.parse(file);
|
||||
} catch (error) {
|
||||
|
@ -0,0 +1,5 @@
|
||||
{
|
||||
// invalid json for test
|
||||
"id": "page:page1",
|
||||
"type": "PageSiderMenu",
|
||||
} []
|
@ -31,7 +31,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash.merge": "4.6.2",
|
||||
"query-string": "6.13.6"
|
||||
"query-string": "6.13.6",
|
||||
"rimraf": "3.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "7.12.1",
|
||||
|
@ -15,9 +15,12 @@
|
||||
*/
|
||||
|
||||
import applyArrayIndices from './applyArrayIndices';
|
||||
import cleanDirectory from './cleanDirectory';
|
||||
import get from './get';
|
||||
import getFileExtension, { getFileSubExtension } from './getFileExtension';
|
||||
import mergeObjects from './mergeObjects';
|
||||
import omit from './omit';
|
||||
import readFile from './readFile';
|
||||
import serializer from './serializer';
|
||||
import set from './set';
|
||||
import stableStringify from './stableStringify';
|
||||
@ -25,12 +28,17 @@ import swap from './swap';
|
||||
import type from './type';
|
||||
import unset from './unset';
|
||||
import urlQuery from './urlQuery';
|
||||
import writeFile from './writeFile';
|
||||
|
||||
export {
|
||||
applyArrayIndices,
|
||||
cleanDirectory,
|
||||
get,
|
||||
getFileExtension,
|
||||
getFileSubExtension,
|
||||
mergeObjects,
|
||||
omit,
|
||||
readFile,
|
||||
serializer,
|
||||
set,
|
||||
stableStringify,
|
||||
@ -38,4 +46,5 @@ export {
|
||||
type,
|
||||
unset,
|
||||
urlQuery,
|
||||
writeFile,
|
||||
};
|
||||
|
@ -15,17 +15,29 @@
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { promisify } from 'util';
|
||||
import type from './type';
|
||||
|
||||
const readFilePromise = promisify(fs.readFile);
|
||||
|
||||
async function readFile(filePath) {
|
||||
if (!type.isString(filePath)) {
|
||||
throw new Error(
|
||||
`Could not read file, file path should be a string, received ${JSON.stringify(filePath)}.`
|
||||
);
|
||||
}
|
||||
if (filePath !== path.resolve(filePath)) {
|
||||
throw new Error(
|
||||
`Could not read file, file path was not resolved, received ${JSON.stringify(filePath)}.`
|
||||
);
|
||||
}
|
||||
try {
|
||||
// By specifying encoding, readFile returns a string instead of a buffer.
|
||||
const file = await readFilePromise(filePath, 'utf8');
|
||||
return file;
|
||||
} catch (error) {
|
||||
if (error.code === 'ENOENT') {
|
||||
if (error.code === 'ENOENT' || error.code === 'EISDIR') {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
@ -16,10 +16,10 @@
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import cleanDirectory from './cleanDirectory';
|
||||
import cleanDirectory from '../src/cleanDirectory';
|
||||
|
||||
test('cleanDirectory', async () => {
|
||||
const dirPath = path.resolve(process.cwd(), 'src/test/cleanDirectory/');
|
||||
const dirPath = path.resolve(process.cwd(), 'test/cleanDirectory/');
|
||||
const filePath1 = path.resolve(dirPath, 'cleanDirectory.txt');
|
||||
const filePath2 = path.resolve(dirPath, 'subdirectory/cleanDirectory.txt');
|
||||
|
@ -14,7 +14,7 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import getFileExtension, { getFileSubExtension } from './getFileExtension';
|
||||
import getFileExtension, { getFileSubExtension } from '../src/getFileExtension';
|
||||
|
||||
test('getFileExtension a', () => {
|
||||
expect(getFileExtension('a')).toBe(null);
|
@ -14,9 +14,9 @@
|
||||
limitations under the License.
|
||||
*/
|
||||
import path from 'path';
|
||||
import readFile from './readFile';
|
||||
import readFile from '../src/readFile';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/readFile');
|
||||
const baseDir = path.resolve(process.cwd(), 'test/readFile');
|
||||
|
||||
test('readFile', async () => {
|
||||
const filePath = path.resolve(baseDir, 'readFile.txt');
|
||||
@ -30,9 +30,14 @@ test('readFile file not found throws', async () => {
|
||||
expect(res).toEqual(null);
|
||||
});
|
||||
|
||||
test('readFile error', async () => {
|
||||
const filePath = baseDir;
|
||||
await expect(readFile(filePath)).rejects.toThrow(
|
||||
'EISDIR: illegal operation on a directory, read'
|
||||
test('readFile error id filepath is not a string', async () => {
|
||||
await expect(readFile({})).rejects.toThrow(
|
||||
'Could not read file, file path should be a string, received {}.'
|
||||
);
|
||||
});
|
||||
|
||||
test('readFile errors if path is not already resolved', async () => {
|
||||
await expect(readFile('./omit.test.js')).rejects.toThrow(
|
||||
'Could not read file, file path was not resolved, received "./omit.test.js".'
|
||||
);
|
||||
});
|
@ -17,9 +17,9 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import rimraf from 'rimraf';
|
||||
import writeFile from './writeFile';
|
||||
import writeFile from '../src/writeFile';
|
||||
|
||||
const baseDir = path.resolve(process.cwd(), 'src/test/getFile');
|
||||
const baseDir = path.resolve(process.cwd(), 'test/writeFile');
|
||||
|
||||
test('writeFile', async () => {
|
||||
const filePath = path.resolve(baseDir, 'writeFile.txt');
|
Loading…
Reference in New Issue
Block a user