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 352c2f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 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
24 changes: 11 additions & 13 deletions scripts/updateLocales.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@

const fs = 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 (require('node:fs').existsSync(localesDirPath)) {
const localesList = await fs.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) => {
Expand Down Expand Up @@ -55,7 +53,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');

Expand Down

0 comments on commit 352c2f7

Please sign in to comment.