ng-add-pug-loader.js   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 19
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 0
eloc 12
nc 1
dl 0
loc 19
rs 10
c 2
b 1
f 0
wmc 5
mnd 1
bc 5
fnc 3
bpm 1.6666
cpm 1.6666
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A ➔ ??? 0 15 3
1
/**
2
   Copyright 2018 June Hanabi
3
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7
8
       http://www.apache.org/licenses/LICENSE-2.0
9
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
 */
16
/**
17
 * Adds the pug-loader inside Angular CLI's webpack config, if not there yet.
18
 * @see https://github.com/danguilherme/ng-cli-pug-loader
19
 */
20
const fs = require('fs');
21
const commonCliConfig = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js';
22
const pugRule = '{ test: /.pug$/, use: [ { loader: "apply-loader" }, { loader: "pug-loader" } ] },';
23
24
fs.readFile(commonCliConfig, (err, data) => {
25
    if (err) { throw err; }
26
27
    const configText = data.toString();
28
    // make sure we don't add the rule if it already exists
29
    if (configText.indexOf(pugRule) > -1) { return; }
30
31
    // Insert the pug webpack rule
32
    const position = configText.indexOf('rules: [') + 8;
33
    const output = [configText.slice(0, position), pugRule, configText.slice(position)].join('');
34
    const file = fs.openSync(commonCliConfig, 'r+');
35
    fs.writeFile(file, output, {}, () => {
36
        fs.close(file, () => { });
37
    });
38
});
39