generated from ryansonshine/typescript-npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read config from .blurhash-maprc.js file
- Loading branch information
Showing
12 changed files
with
338 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env node | ||
|
||
const nodemon = require('nodemon'); | ||
import { program } from 'commander'; | ||
import { existsSync, readFileSync } from 'fs'; | ||
import { resolve } from 'path'; | ||
import { BlurHashMap, BlurHashMapConfig } from '../src'; | ||
import { | ||
ComponentRange, | ||
HashMapJsonSaveToTarget, | ||
zBlurHashMapConfig, | ||
} from '../src/blur-hash-map'; | ||
|
||
const RC_CONFIG = resolve('.blurhash-maprc.js'); | ||
|
||
interface CommanderActionOptions { | ||
assets: string; | ||
extensions: string; | ||
componentX?: ComponentRange; | ||
componentY?: ComponentRange; | ||
targetJson?: HashMapJsonSaveToTarget; | ||
} | ||
const packageJson: Record<string, string> = JSON.parse( | ||
readFileSync('./package.json', 'utf-8') | ||
); | ||
|
||
const watchChanges = async (options: BlurHashMapConfig): Promise<void> => { | ||
const blurHashGenerator = new BlurHashMap(options); | ||
return blurHashGenerator.init().then(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
nodemon({ | ||
watch: options.assetsRoot, | ||
ext: [...(options.imageExtensions || []), 'hash'].join(''), | ||
}) | ||
.on('quit', function () { | ||
throw 'nodemon exited'; | ||
}) | ||
.on('restart', function (changedFiles = []) { | ||
for (const file of changedFiles) { | ||
blurHashGenerator.generateOrDelete(file); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
const readConfig = (): Promise<BlurHashMapConfig> => { | ||
return new Promise((res, rej) => { | ||
if (existsSync(RC_CONFIG)) { | ||
void import(RC_CONFIG).then((config: Record<string, unknown>) => { | ||
if ('default' in config) { | ||
res(zBlurHashMapConfig.parse(config.default)); | ||
} else { | ||
rej('`blurhash-maprc.js` must have default export'); | ||
} | ||
}); | ||
} else { | ||
rej(); | ||
} | ||
}); | ||
}; | ||
|
||
const readCommandParams = () => { | ||
program | ||
.version(packageJson.version) | ||
.option('-a, --assets <assets>', 'Required. Set path to your assets folder') | ||
.option( | ||
'-e, --extensions <extensions>', | ||
'Optional. Define image file extensions. Default: jpg,jpeg,png,bmp,webp' | ||
) | ||
.option('-x, --componentX <componentX>', 'Optional. Default : 4') | ||
.option('-y, --componentY <componentY>', 'Optional. Default : 3') | ||
.option('-t, --target <targetJson>', 'Optional. Default : project root') | ||
.action((options: CommanderActionOptions) => { | ||
if (typeof options.assets !== 'string') { | ||
throw Error( | ||
"Please define assets paths to be watched with the '-a' key" | ||
); | ||
} | ||
if (typeof options.assets !== 'string') { | ||
throw Error( | ||
"Please define assets paths to be watched with the '-a' key" | ||
); | ||
} | ||
|
||
const config: BlurHashMapConfig = { | ||
assetsRoot: options.assets, | ||
imageExtensions: BlurHashMap.toAllowedImageTypeList(options.extensions), | ||
targetJson: options.targetJson, | ||
components: { x: options.componentX, y: options.componentY }, | ||
}; | ||
void watchChanges(config); | ||
}); | ||
program.parse(process.argv); | ||
}; | ||
|
||
readConfig() | ||
.then(watchChanges) | ||
.catch(() => { | ||
readCommandParams(); | ||
}); |
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.