Skip to content

Commit

Permalink
fix(scripts): uses the node_modules package as the source and adds th…
Browse files Browse the repository at this point in the history
…e call to package.json
  • Loading branch information
Alex Komz authored and Alex Komz committed Jan 25, 2024
1 parent 9001bf0 commit 2918e79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"test:watch": "jest --watchAll",
"typecheck": "tsc --noEmit",
"prepublishOnly": "npm run lint && npm run test && npm run build",
"updateLocales": "node scripts/updateLocales.js",
"prepare": "husky install"
},
"files": [
Expand Down
33 changes: 16 additions & 17 deletions scripts/updateLocales.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@
* @module Locale
* */

const fs = require('node:fs/promises');
const fs = require('node:fs');
const fsPromises = require('node:fs/promises');

const VALID_FILE_PATTERN = /^((?!index).)*.js$/;
const LOCALES_PATH = 'src/settings/locales.ts';
const GITHUB_LOCALES_URL = 'https://github.com/iamkun/dayjs/tree/dev/src/locale';
const LOCALES_DIR_PATH = 'node_modules/dayjs/locale';

const fetchLocalesList = async (githubUrl) => {
try {
const res = await fetch(githubUrl);
const json = await res.json();
const {items} = json.payload.tree;

return items.map((item) => item.name);
} catch (error) {
throw new Error(
`Something went wrong when trying to retrieve data from github dayjs, check if the URL is correct ${GITHUB_LOCALES_URL}`,
);
const getLocalesList = async (localesDirPath) => {
if (fs.existsSync(localesDirPath)) {
const localesList = await fsPromises.readdir(localesDirPath);
return localesList.filter((locale) => VALID_FILE_PATTERN.test(locale));
}

throw new Error(
'The script was called before the dayjs library was installed, install it and try again',
);
};

const createLocalesFile = async (localesPath) => {
if (require('node:fs').existsSync(localesPath)) {
await fs.rm(localesPath);
if (fs.existsSync(localesPath)) {
await fsPromises.rm(localesPath);
}

return await fs.open(localesPath, 'w');
return await fsPromises.open(localesPath, 'w');
};

const buildLocalesContent = (localesList) => {
Expand All @@ -55,7 +54,7 @@ const buildLocalesContent = (localesList) => {

(async function () {
try {
const localesList = await fetchLocalesList(GITHUB_LOCALES_URL);
const localesList = await getLocalesList(LOCALES_DIR_PATH);

console.info('Locales loaded successfully');

Check warning on line 59 in scripts/updateLocales.js

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement

Expand Down

0 comments on commit 2918e79

Please sign in to comment.