1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright 2019 highstreet technologies GmbH and others...
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 const fs = require('fs');
19 const args = process.argv.slice(2);
20 const inputFile = __dirname + '/../../' + args[0];
21 const target = __dirname + '/../../target';
23 const deleteFolderRecursive = function(path) {
24 if (fs.existsSync(path)) {
25 fs.readdirSync(path).forEach(function(file, index){
26 var curPath = path + "/" + file;
27 if (fs.lstatSync(curPath).isDirectory()) { // recurse
28 deleteFolderRecursive(curPath);
29 } else { // delete file
30 fs.unlinkSync(curPath);
37 if (!fs.existsSync(inputFile)) {
38 throw new Error(['Input file:', inputFile, 'not found!'].join(' '));
41 if (fs.existsSync(target)) {
43 deleteFolderRecursive(target);
48 fs.mkdirSync(target, { recursive: true });
49 fs.readFile(inputFile, "utf8", function (err, data) {
51 return console.log(err);
56 var filename = target + '/';
58 var modules = data.split(/\r?\n/).forEach(function (line) {
60 if (line.startsWith("submodule ") || line.startsWith("module ")) {
62 filename = target + "/" + line.split(' ')[1] + '.yang';
65 if (collect === true) {
68 if (line.startsWith("}")) {
70 fs.writeFile(filename, file.join("\n"), function (err) {
72 return console.log(err);
74 console.log(["The file", filename, "was saved!"].join(' '));