UNPKG

2.15 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/sink
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.IniFile = void 0;
12const mrm_core_1 = require("mrm-core");
13const KeyValuePair_1 = require("../Base/KeyValuePair");
14/**
15 * Ini file to work with files like `.editorconfig`.
16 *
17 * ```ts
18 * const ini = new Ini(__dirname, '.editorconfig')
19 * ini.set('_global', { root: true })
20 * ini.set('**.js', { insert_final_newline: true })
21 *
22 * ini.commit()
23 * ```
24 */
25class IniFile extends KeyValuePair_1.KeyValuePair {
26 constructor(basePath, filename) {
27 super(basePath);
28 /**
29 * The `ini` function from `mrm-core` relies on the current
30 * working directory, that's why we have to cd in to the
31 * base path before creating a new instance of it.
32 */
33 this.cdIn();
34 this.filePointer = (0, mrm_core_1.ini)(filename);
35 this.cdOut();
36 }
37 /**
38 * Handling the onmerge action. This method is called by
39 * the `commit` method.
40 */
41 onmerge(lifecycle, body) {
42 if (lifecycle === 'commit') {
43 this.filePointer.set(body.section, Object.assign({}, this.get(body.section), body.values));
44 return true;
45 }
46 if (lifecycle === 'rollback') {
47 const resetObject = Object.keys(body.values).reduce((result, key) => {
48 result[key] = undefined;
49 return result;
50 }, {});
51 this.filePointer.set(body.section, Object.assign({}, this.get(body.section), resetObject));
52 return true;
53 }
54 }
55 /**
56 * Merge to the section values of an ini file.
57 *
58 * @example
59 * ```ts
60 * ini.merge('root', { indent_style: space })
61 * ```
62 */
63 merge(section, values) {
64 if (typeof values !== 'object' || values === null) {
65 return this;
66 }
67 this.addAction('merge', { section, values });
68 return this;
69 }
70}
71exports.IniFile = IniFile;