config-parser-master
TypeScript icon, indicating that this package has built-in type declarations

0.5.1 • Public • Published

config-parser-master

Description

This is a simple config parser that can read and write to a config file.

Currently, it only supports reading and writing to a config file in the following formats:

  • js、mjs、ts、mts(export default)
  • cjs(module.exports)
  • json、rc(json)
  • yml、yaml

Additionally, it also supports reading javascript or typescript files. (including jsx and tsx)

Install

$ npm install config-parser-master

Simple Usage

import { ConfigParser } from 'config-parser-master';

const config = ConfigParser.parse('./package.json')
config.put('scripts.hi', 'echo hi')
config.save()

ParseJs Exammple

If you want to parse a javascript or typescript file, you can use the parseJs or parse function, the difference is that the parseJs function will return a BaseJSConfig object, which has some additional functions.

If your javascript or typescript file isn't a config file, you can set the skipCheck parameter to true, and it will not check if the file is a config file.

Parse a source file

/*
file: main.js
---
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

createApp(App).mount('#app')

function sum(a, b) {
  return a + b
}

sum(1, 2)
*/
import { ConfigParser } from 'config-parser-master';

const config = ConfigParser.parseJs('main.js', true)

// import
config.import('main.css') // import "main.css";
config.import('Home.vue', { defaultKey: 'Home' }) // import Home from "Home.vue";
config.import('config-parser-master', { keys: ['ConfigParser'] }) // import { ConfigParser } from "config-parser-master";
config.import('some-package', { defaultKey: 'defaultKey', keys: ['a', 'b'] }) // import defaultKey, { a, b } from "some-package";

// isContainCallExpression
console.log(config.isContainCallExpression('createApp()')) // true
console.log(config.isContainCallExpression('createApp().mount()')) // true
console.log(config.isContainCallExpression('sum()')) // true
console.log(config.isContainCallExpression('add()')) // false

console.log(config.content)
/*
import defaultKey, { a, b } from "some-package";
import { ConfigParser } from "config-parser-master";
import Home from "Home.vue";
import "main.css";
import { createApp } from 'vue';
import './style.css';
import App from './App.vue';
createApp(App).mount('#app');
function sum(a, b) {
  return a + b;
}
sum(1, 2);
*/

Parse a config file

/*
file: config.js
---
import { svelte } from '@sveltejs/vite-plugin-svelte'

function sum(a, b) {
  return a + b
}

export default {
  plugins: [svelte(), sum(1, 2)],
}
*/
const config = ConfigParser.parseJs('config.js')
const plugins = config.get('plugins') as ParserValueType[]

// isSameCallExpression and isStrictSameCallExpression
console.log(
  plugins.map((plugin) => config.isSameCallExpression(plugin, 'svelte()'))
) // [true, false]
console.log(
  plugins.map((plugin) => config.isSameCallExpression(plugin, 'sum()'))
) // [false, true]
console.log(
  plugins.map((plugin) => config.isStrictSameCallExpression(plugin, 'sum()'))
) // [false, false]
console.log(
  plugins.map((plugin) =>
    config.isStrictSameCallExpression(plugin, 'sum()', [1, 2])
  )
) // [false, true]

// getCallExpressionArgs
console.log(plugins.map((plugin) => config.getCallExpressionArgs(plugin))) // [[], [1, 2]]

// createCallExpression and put
const plugin = config.createCallExpression('plugin', [1, 2, 3])
plugins.push(plugin)
config.put('plugins', plugins)

console.log(config.content)
/*
import { svelte } from '@sveltejs/vite-plugin-svelte';
function sum(a, b) {
  return a + b;
}
export default {
  plugins: [svelte(), sum(1, 2), plugin(1, 2, 3)]
};
*/

API

ConfigParser

  • ConfigParser.parse(path: string): BaseConfig
    • Parse a config file and return a Config object.
  • ConfigParser.parseJs(path: string, skipCheck?: boolean = false): BaseJSConfig
    • Parse a javascript or typescript file and return a Config object.

BaseConfig

  • BaseConfig.put(key: string, value: ParserValueType): BaseConfig
    • Put a value into the config object.
  • BaseConfig.get(key: string, defaultValue?: ParserValueType | ParserValueType[]): ParserValueType
    • Retrieve a value from the config object.
  • BaseConfig.delete(key: string): BaseConfig
    • Delete a value from the config object.
  • async Config.save(): Promise<void>
    • Save the config object to the config file.

BaseJSConfig

  • Extends BaseConfig
  • import(source: string, options: { defaultKey?: string, keys?: string[] } = {}): BaseJSConfig
    • Insert 「import defaultKey, { ...keys } from source」 to the config file.
  • require(source: string, options: { defaultKey?: string, keys?: string[] } = {}): BaseJSConfig
    • Insert 「const defaultKey, { ...keys } = require(source)」 to the config file.
  • createCallExpression(name: string, args?: ParserValueType[]): t.CallExpression
    • Create a call expression.
  • isStrictSameCallExpression(callExpression: t.Expression, name: string, args?: ParserValueType[]): boolean
    • Check if the call expression is strict same (including name and args).
  • isSameCallExpression(callExpression: t.Expression, name: string): boolean
    • Check if the call expression is same.
  • getCallExpressionArgs(callExpression: t.Expression): ParserValueType[]
    • Get the arguments of the call expression.
  • isContainCallExpression(name: string, args?: ParserValueType[]): boolean
    • Check if the config file contains the call expression.

Propterites

BaseConfig.file: string

The path of the config file.

BaseConfig.content: string

The content of the config file.

Types

ParserValueType

type ParserValueType =
  | string
  | number
  | boolean
  | t.CallExpression
  | ParserValueType[]
  | { [key: string]: ParserValueType }

In simple terms, configuration values can be strings, numbers, boolean, t.CallExpression values, arrays, or objects.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.5.1-dev-9443f6432-202307280dev
0.5.10latest

Version History

VersionDownloads (Last 7 Days)Published
0.5.10
0.5.1-dev-9443f6432-202307280
0.5.00
0.5.0-dev-038779932-202307270
0.5.0-dev-cfbdc8342-202307270
0.4.20
0.4.2-dev-df870c696-202307270
0.4.1-dev-311530e84-202307270
0.4.10
0.4.1-dev-15625414d-202307270
0.4.00
0.4.0-dev-8cbd0f19b-202307220
0.4.0-dev-f0e9cc43f-202307220
0.4.0-dev-3b32c0c4c-202307220
0.4.0-dev-292a5cebf-202307220
0.4.0-dev-8a1ff265f-202307220
0.4.0-dev-1e925f0a7-202307220
0.4.0-dev-06ba3fccb-202307220
0.4.0-dev-c297ed4fa-202307220
0.4.0-dev-815adf1ba-202307220
0.3.00
0.3.0-dev-c3d2400f7-202307210
0.2.0-dev-6d75ebe49-202307210
0.1.30
0.1.3-dev-e5f1938c8-202307190
0.1.2-dev-0efeb0345-202307140
0.1.20
0.1.2-dev-e0265f91d-202307140
0.1.10
0.1.1-dev-88fd8eb0e-202307130
0.1.00
0.1.0-dev-1b7a588ed-202307020
0.1.0-678f50ace-202307020
0.1.0--202307020

Package Sidebar

Install

npm i config-parser-master

Weekly Downloads

0

Version

0.5.1

License

MIT

Unpacked Size

866 kB

Total Files

4

Last publish

Collaborators

  • laijunbin