0.1.3 • Published 1 year ago

@doomd/scripts v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Doomd Scripts

Overview

This package will contain the simple node scripts that I find useful across my other (mostly Svelte/Sveltekit) projects. I got tired of copying and pasting code from one project to another...this is obviously the smarter way. It will be slow-going adding all my scripts because most of them need to be refactored to be more universal in scope before I can add them here.

Installation

For now, you problably only need to install this as a dev Dependancy:

[p]npm i -D doomd-scripts

Scripts

doomd-insert-version

This script will generate a file that exports two const's (version and versionTS) which makes it easy to use these variables in your src files (in the footer of your page for example). The version number is copied from your package.json file. The file's contents will look like this:

// version.ts

export const version = '0.0.15'
export const versionTS = '2022-01-11T01:25:49.564Z'

The default output path is src/version.ts, but you can simply add the desired path (relative to the project's root directory) immediately after the command if you'd like to change it. For example, (from your project's root directory):

npx doomd-insert-version
# outputs to 'src/version.ts'

npx doomd-insert-version 'src/lib/site-details/version.ts'
# outputs to 'src/lib/site-details/version.ts'

Or you can add it to your package.json's scripts property before your build (so your source files can use the generated file's exports as variables). For example:

// package.json example usage:

"scripts": {
  "dev": "doomd-insert-version && svelte-kit dev",
  "prebuild": "doomd-insert-version",
  "build": "svelte-kit build",
}

After you've run a build, you might import the following into your index.ts file (for example):

// src/index.js

import { version, versionTS } from './version'
console.log(version)

// outputs: '0.0.15' (or whatever your pkg version is)

doomd-create-component-index

Running this script inside a Sveltekit project, will automatically create a list of all your svelte components and create an exports index file for you. This is particularly useful if you plan to package your components for npm publishing.

Running the default script without any arguments will look in src/lib for your components, and will output an index file at src/lib/index.ts. CAUTION: This will OVERWRITE any existing index files in the same path.

If your src directory looks like this:

src/
┣ lib/
┃ ┣ components/
┃ ┃ ┣ complicated/
┃ ┃ ┃ ┣ CoolComponent.d.ts
┃ ┃ ┃ ┣ CoolComponent.svelte
┃ ┃ ┃ ┣ CoolComponent.ts
┃ ┃ ┃ ┗ _Internal.svelte
┃ ┃ ┗ SimpleComponent.svelte
┗ routes/

Output will look like this:

// src/lib/index.ts
export { default as CoolComponent } from './components/complicated/CoolComponent.svelte'
export { default as SimpleComponent } from './components/SimpleComponent.svelte'

Default Arguments:

--srcpath='src/lib' # root directory of your components and where index file will be generated.
--from='.' # relative directory of components fromthe generated index file.
--internal  # Include internal component files that start with an "_"? Default is ommitted (false). Include the flag and it will be set to true.
--js # Index file extension. Default is ommitted (false) = 'index.ts'. Include the flag and it will be set to true = 'index.js'.

Example of command with arguments:

npx doomd-create-comp-index --srcpath='src/lib/components' --js

This will output the index file to src/lib/components/index.js

Links

Credits

Special thanks to:

  • Sensedeep for their guidance on publishing cjs and es6 modules with one codebase.