Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: When does the available locales array get created in the file system? #1

Closed
adammcarth opened this issue Apr 19, 2020 · 5 comments

Comments

@adammcarth
Copy link

Hi Anton,

I came here from this comment - thanks for creating and sharing your package with us.

I have a question that I haven't been able to easily determine by looking through the code.

When does the res folder get created (at which point the momentjs locales files is also made)? I was thinking it might have been a postinstall script in the package.json file, but I couldn't see anything.

Is it up to the developers to manually run the script which creates this res folder/files as part of their build steps?

@adammcarth adammcarth changed the title Question: When does the res folder get created? Question: When does the res folder get created? (moment-utl) Apr 19, 2020
@adammcarth adammcarth changed the title Question: When does the res folder get created? (moment-utl) Question: When does the available locales array get created in the file system? Apr 19, 2020
@tonix-tuft
Copy link
Owner

tonix-tuft commented Apr 19, 2020

@adammcarth Thank you for your interest in this repo Adam.

Right now, when you run npm install --save moment moment-utl, npm will download a build of moment-utl and inside the node_modules/moment-utl/res folder of your consuming project you will find a locales.js file prebuilt for you containing the locales array and object/map.

It is important to understand that this locales.js file is automatically created when I publish the moment-utl package to the NPM registry thanks to the build-locales script defined in the scripts section of the package.json file.

But as I create that file whenever I build and publish a new version of the package, I am using a dev dependency of moment to retrieve the locales. This moment dev dependency I am using at build time may not have the same version as the moment dependency you end up with when you install it in your project alongside with moment-utl (by the way, moment-utl peer depends on moment).

Therefore, as of today, moment-utl dev-depends on "moment": "^2.24.0", but it may happen that when you run npm install --save moment moment-utl in your project, you end up with a version of moment having a different/newer minor or patch version and which, who knows, may have a new locale file under node_modules/moment/locale which wasn't there before (let's pretend it to be node_modules/moment/locale/new-locale.js).

If that's the case, that new-locale locale code won't be seen by moment-utl, simply because res/locales.js was already there and was generated during deployment to the NPM registry with another version of moment.

Is it up to the developers to manually run the script which creates this res folder/files as part of their build steps?

Yes, if you want to be sure that the node_modules/moment-utl/res/locales.js file stays up-to-date with your project's moment, then moment-utl exposes a moment-utl-locales script for you which you can run with npx moment-utl-locales or ./node_modules/.bin/moment-utl-locales as part of your build steps before you transpile and bundle your code with Webpack, Rollup or whatever you are using. This will work only if you are using ES6, though.

npx moment-utl-locales just recreates the node_modules/moment-utl/res/locales.js file using your moment package, which is the one moment-utl peer depends on and which was installed by you with npm install --save moment moment-utl.

I was thinking it might have been a postinstall script in the package.json file, but I couldn't see anything.

Well, I wasn't aware that there is a postinstall script functionality, sounds interesting.

Could you please provide a resource for that? Is it a feature supported both by NPM and yarn?
I will look at it, because if it is possible to run npx moment-utl-locales automatically whenever moment-utl is installed with npm install --save moment moment-utl or whenever moment or moment-utl are updated with npm update, then there will be no need to bundle moment-utl with a prebuilt res/locales.js file and the client code using moment-utl won't need to add npx moment-utl-locales as part of their build steps.

Anyway, I am on my way to document this package with a README, I am just searching for a good JSDoc to Markdown generator so that I can automate this process. If you could advise a tool for this, it would be helpful too!

I hope I was esplicative and clear enough.
Feel free to tell me if there's something you didn't understand and I will try to rephrase this comment.

If you have an idea and want to contribute to this, for now, simple and tiny project, that will also be appreciated.

Thank you!

@tonix-tuft
Copy link
Owner

@adammcarth I have added a README file. Let me know if you find it clear.

@tonix-tuft
Copy link
Owner

@adammcarth I plan to close this issue, please, let me know if there is anything else I could clarify for you about how the creation of the locales with npx moment-utl-locales works.

@adammcarth
Copy link
Author

adammcarth commented Apr 28, 2020

Sorry for taking so long to get back to you @tonix-tuft!

You answered all my questions, so feel free to close this :)

I personally think that generating the locales automatically on postinstall is a good future idea because it makes the process more dynamic, and less dependant on this package being released frequently in order to keep up with changes to Moment.js.

You can checkout the (limited) documentation here. It's valid for npm and yarn.

It's actually very simple, you just place a command inside your package.json and it will automatically get executed after your package is installed.

// package.json
{
  "scripts": {
    ...
    "generate-locales": "...",
    "postinstall": "generate-locales"
  }
}

I would definitely consider using this package for our React Native apps if the process of generating that locale file was more future proof!

Cheers,

Adam.

@tonix-tuft
Copy link
Owner

Thank you Adam!

I will check the link you posted.

The only doubt I have is that postinstall, as I understand, executes after moment-utl is installed, but not after moment-utl has been installed and we are sure that moment was already installed before it too.

What happens if the moment package is not installed yet when someone installs moment-utl?
Executing the script in such cases would fail because moment is not there yet as it is a peer dependency of moment-utl and it may happen that the consumer of the package runs
npm i moment-utl alone and then npm i moment only after seeing the NPM's peer dependency warning.

Also, moment may update when the consumer of the package runs npm update and in that case it would also be great to regenerate the locales. As far as I understand, though, there's not such thing as a postupdate script in NPM...

For now, I am closing this issue, if I come up with something I will write another comment here.

I personally think that generating the locales automatically on postinstall is a good future idea because it makes the process more dynamic, and less dependant on this package being released frequently in order to keep up with changes to Moment.js.

Note that if you add npx moment-utl-locales or ./node_modules/.bin/moment-utl-locales
as part of your build process before you bundle your code, the entire process is dynamic and you will always end up with the up-to-date locales with your installed version of moment.
At least for now, this seems to be the most reliable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants