UNPKG

6.99 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.AdonisRcFile = void 0;
12const Json_1 = require("../Formats/Json");
13/**
14 * Exposes API to mutate the contents of `.adonisrc.json` file.
15 */
16class AdonisRcFile extends Json_1.JsonFile {
17 constructor(basePath) {
18 super(basePath, '.adonisrc.json');
19 /**
20 * Storing a local copy of preloads for concatenating
21 * new entries.
22 */
23 this.preloads = [];
24 /**
25 * Storing a local copy of metaFiles for concatenating
26 * new entries.
27 */
28 this.metaFiles = [];
29 /**
30 * Storing a local copy of commands for concatenating
31 * new entries.
32 */
33 this.commands = [];
34 /**
35 * Storing a local copy of providers for concatenating
36 * new entries.
37 */
38 this.providers = [];
39 /**
40 * Storing a local copy of aceProviders for concatenating
41 * new entries.
42 */
43 this.aceProviders = [];
44 /**
45 * Storing a local copy of testProviders for concatenating
46 * new entries.
47 */
48 this.testProviders = [];
49 this.preloads = this.get('preloads', []);
50 this.metaFiles = this.get('metaFiles', []);
51 this.commands = this.get('commands', []);
52 this.providers = this.get('providers', []);
53 this.aceProviders = this.get('aceProviders', []);
54 this.testProviders = this.get('testProviders', []);
55 }
56 /**
57 * Handle `preloads` in a custom way on rollback, since the `mrm-core` uses
58 * `lodash.unset` which replaces the array index value with `null` and
59 * we instead want to remove the index value completely.
60 */
61 onset(lifecycle, body) {
62 if (lifecycle === 'rollback') {
63 let key = null;
64 if (body.key.startsWith('preloads')) {
65 key = 'preloads';
66 }
67 if (body.key.startsWith('metaFiles')) {
68 key = 'metaFiles';
69 }
70 if (body.key.startsWith('commands')) {
71 key = 'commands';
72 }
73 if (body.key.startsWith('providers')) {
74 key = 'providers';
75 }
76 if (body.key.startsWith('aceProviders')) {
77 key = 'aceProviders';
78 }
79 if (body.key.startsWith('testProviders')) {
80 key = 'testProviders';
81 }
82 if (!key) {
83 return;
84 }
85 const index = body.key.split('[')[1].replace(/\]/g, '');
86 this.get(key, []).splice(index, 1);
87 return true;
88 }
89 }
90 /**
91 * Set the exception handler namespace.
92 */
93 setExceptionHandler(namespace) {
94 this.set('exceptionHandlerNamespace', namespace);
95 return this;
96 }
97 /**
98 * Set the preload file to the `.adonisrc.json` file.
99 */
100 setPreload(filePath, environment, optional) {
101 let preloadIndex = this.preloads.findIndex((preload) => {
102 if (preload.file) {
103 return preload.file === filePath;
104 }
105 return preload === filePath;
106 });
107 preloadIndex = preloadIndex === -1 ? this.preloads.length : preloadIndex;
108 let preloadEntry = {
109 file: filePath,
110 };
111 /**
112 * Set the environment when it exists
113 */
114 if (environment && environment.length) {
115 preloadEntry.environment = environment;
116 }
117 /**
118 * Set the optional property when it exists
119 */
120 if (optional !== undefined) {
121 preloadEntry.optional = optional;
122 }
123 /**
124 * Set preload entry as string, when it doesn't have explicit environment
125 * and optional fields.
126 */
127 if (preloadEntry.optional === undefined && preloadEntry.environment === undefined) {
128 preloadEntry = preloadEntry.file;
129 }
130 this.preloads[preloadIndex] = preloadEntry;
131 this.set(`preloads[${preloadIndex}]`, preloadEntry);
132 return this;
133 }
134 /**
135 * Set IoC container aliases
136 */
137 setAlias(namespace, autoloadPath) {
138 this.set(`aliases.${namespace}`, autoloadPath);
139 return this;
140 }
141 /**
142 * Set custom directory
143 */
144 setDirectory(key, value) {
145 this.set(`directories.${key}`, value);
146 return this;
147 }
148 /**
149 * Add custom file to `metaFiles` array.
150 */
151 addMetaFile(filePath, reloadServer) {
152 let entryIndex = this.metaFiles.findIndex((file) => {
153 if (file.pattern) {
154 return file.pattern === filePath;
155 }
156 return file === filePath;
157 });
158 entryIndex = entryIndex === -1 ? this.metaFiles.length : entryIndex;
159 const entry = reloadServer === false
160 ? {
161 pattern: filePath,
162 reloadServer: false,
163 }
164 : filePath;
165 this.metaFiles[entryIndex] = entry;
166 this.set(`metaFiles[${entryIndex}]`, entry);
167 }
168 /**
169 * Add new commands to the commands array
170 */
171 addCommand(commandPath) {
172 let entryIndex = this.commands.findIndex((command) => {
173 return command === commandPath;
174 });
175 entryIndex = entryIndex === -1 ? this.commands.length : entryIndex;
176 this.commands[entryIndex] = commandPath;
177 this.set(`commands[${entryIndex}]`, commandPath);
178 }
179 /**
180 * Add new providers to the providers array
181 */
182 addProvider(provider) {
183 let entryIndex = this.providers.findIndex((command) => {
184 return command === provider;
185 });
186 entryIndex = entryIndex === -1 ? this.providers.length : entryIndex;
187 this.providers[entryIndex] = provider;
188 this.set(`providers[${entryIndex}]`, provider);
189 }
190 /**
191 * Add new providers to the ace providers array
192 */
193 addAceProvider(provider) {
194 let entryIndex = this.aceProviders.findIndex((command) => {
195 return command === provider;
196 });
197 entryIndex = entryIndex === -1 ? this.aceProviders.length : entryIndex;
198 this.aceProviders[entryIndex] = provider;
199 this.set(`aceProviders[${entryIndex}]`, provider);
200 }
201 /**
202 * Add new providers to the test providers array
203 */
204 addTestProvider(provider) {
205 let entryIndex = this.testProviders.findIndex((command) => {
206 return command === provider;
207 });
208 entryIndex = entryIndex === -1 ? this.testProviders.length : entryIndex;
209 this.testProviders[entryIndex] = provider;
210 this.set(`testProviders[${entryIndex}]`, provider);
211 }
212}
213exports.AdonisRcFile = AdonisRcFile;