|
1 | 1 | 'use strict'; |
2 | 2 | const _ = require('lodash'); |
3 | 3 | const utils = require('../../utils.js'); |
| 4 | +const jsonfile = require('jsonfile'); |
| 5 | +const jsonQuery = require('json-query'); |
| 6 | +const fs = require('fs-extra'); |
4 | 7 |
|
5 | 8 | module.exports = function(AngularATGenerator) { |
6 | 9 |
|
7 | | - AngularATGenerator.prototype.copyCopmFiles = function copyFiles() { |
8 | | - // setting defaults, component name and path settings |
9 | | - let fullPath = this.props.componentName; |
10 | | - let pathAsArray = fullPath.split('/'); |
11 | | - let componentName = pathAsArray[pathAsArray.length - 1]; |
12 | | - let parentName = null; |
13 | | - let parentPath = null; |
14 | | - let isDuplicate = false; |
15 | | - // data to be passed to templates and used to get info |
16 | | - const data = { |
17 | | - 'componentName': componentName, |
18 | | - 'componentNameCamel': _.camelCase(componentName), |
19 | | - 'controllerName': _.upperFirst(_.camelCase(componentName)), |
20 | | - 'componentModule': _.camelCase(componentName) |
21 | | - }; |
22 | | - //if the component has no parent |
23 | | - if (pathAsArray.length === 1) { |
24 | | - try { |
25 | | - const indexModulesWriteLine = "require('./components/" + data.componentName + "/" + data.componentName + ".module').name,"; |
26 | | - utils.addToFile('index.components.js', indexModulesWriteLine, utils.COMPONENT_MARKER, this.destinationRoot() + '/src/app'); |
27 | | - } catch (err) { |
28 | | - this.log('Could not generate this item due to missing file structure.'); |
29 | | - return; |
30 | | - } |
31 | | - } else { |
32 | | - //if the component is nested in a parent component |
33 | | - parentName = pathAsArray[pathAsArray.length - 2]; |
34 | | - // if single parent, join by '/' else join by '/components/' to nest within the parent components |
35 | | - const joinString = (pathAsArray.length>2)?'/components/':'/'; |
36 | | - parentPath = _.join(pathAsArray.slice(0, pathAsArray.length - 1), joinString); |
37 | | - fullPath = parentPath + '/components/' + data.componentName; |
38 | | - // importing files to parent component |
39 | | - try { |
40 | | - //module |
41 | | - const moduleImport = "import * as " + data.componentModule + " from './components/" + componentName + '/' + componentName + ".module';"; |
42 | | - utils.addToFile(parentName + '.module.js', moduleImport, utils.IMPORT_MODULE_MARKER, this.destinationRoot() + '/src/app/components/' + parentPath); |
43 | | - //dependency |
44 | | - const dependencyImport = "'" + data.componentModule + "',"; |
45 | | - utils.addToFile(parentName + '.module.js', dependencyImport, utils.IMPORT_DEPENDENCY_MARKER, this.destinationRoot() + '/src/app/components/' + parentPath); |
46 | | - } catch (err) { |
47 | | - this.log('Parent component files not found.'); |
48 | | - return; |
49 | | - } |
50 | | - } |
| 10 | + AngularATGenerator.prototype.copyCopmFiles = function copyFiles() { |
| 11 | + // setting defaults, component name and path settings |
| 12 | + let fullPath = this.props.componentName; |
| 13 | + let pathAsArray = fullPath.split('/'); |
| 14 | + let componentName = pathAsArray[pathAsArray.length - 1]; |
| 15 | + let parentName = null; |
| 16 | + let parentPath = null; |
| 17 | + let isDuplicate = false; |
| 18 | + // data to be passed to templates and used to get info |
| 19 | + const data = { |
| 20 | + 'componentName': componentName, |
| 21 | + 'componentNameCamel': _.camelCase(componentName), |
| 22 | + 'controllerName': _.upperFirst(_.camelCase(componentName)), |
| 23 | + 'componentModule': _.camelCase(componentName) |
| 24 | + }; |
| 25 | + |
| 26 | + // if (this.options.r) { |
| 27 | + // |
| 28 | + // if (pathAsArray.length === 1) { |
| 29 | + // try { |
| 30 | + // const indexModulesRemoveLine = "require('./components/" + data.componentName + "/" + data.componentName + ".module').name,"; |
| 31 | + // utils.removeLineFromFile('index.components.js', indexModulesRemoveLine, this.destinationRoot() + '/src/app') |
| 32 | + // utils.deleteDirRecursive(this.destinationRoot() + '/src/app/components/' + data.componentName); |
| 33 | + // } catch (err) {} |
| 34 | + // }else{ |
| 35 | + // parentName = pathAsArray[pathAsArray.length - 2]; |
| 36 | + // |
| 37 | + // // //module |
| 38 | + // // const moduleImportRemoveLine = "import * as " + data.componentModule + " from './components/" + componentName + '/' + componentName + ".module';"; |
| 39 | + // // utils.addToFile(parentName + '.module.js', moduleImport, utils.IMPORT_MODULE_MARKER, this.destinationRoot() + '/src/app/components/' + parentPath); |
| 40 | + // // //dependency |
| 41 | + // // const dependencyImport = "'" + data.componentModule + "',"; |
| 42 | + // // utils.addToFile(parentName + '.module.js', dependencyImport, utils.IMPORT_DEPENDENCY_MARKER, this.destinationRoot() + '/src/app/components/' + parentPath); |
| 43 | + // |
| 44 | + // } |
| 45 | + // return; |
| 46 | + // } |
51 | 47 |
|
52 | | - // checking if the module exists, if so it is a duplicate |
53 | | - isDuplicate = utils.existsSync(this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.module.js')); |
| 48 | + //if the component has no parent |
| 49 | + if (pathAsArray.length === 1) { |
| 50 | + try { |
| 51 | + const indexModulesWriteLine = "require('./components/" + data.componentName + "/" + data.componentName + ".module').name,"; |
| 52 | + utils.addToFile('index.components.js', indexModulesWriteLine, utils.COMPONENT_MARKER, this.destinationRoot() + '/src/app'); |
| 53 | + } catch (err) { |
| 54 | + this.log('Could not generate this item due to missing file structure.'); |
| 55 | + return; |
| 56 | + } |
| 57 | + } else { |
| 58 | + //if the component is nested in a parent component |
| 59 | + parentName = pathAsArray[pathAsArray.length - 2]; |
| 60 | + // if single parent, join by '/' else join by '/components/' to nest within the parent components |
| 61 | + const joinString = (pathAsArray.length > 2) |
| 62 | + ? '/components/' |
| 63 | + : '/'; |
| 64 | + parentPath = _.join(pathAsArray.slice(0, pathAsArray.length - 1), joinString); |
| 65 | + fullPath = parentPath + '/components/' + data.componentName; |
| 66 | + // importing files to parent component |
| 67 | + try { |
| 68 | + //module |
| 69 | + const moduleImport = "import * as " + data.componentModule + " from './components/" + componentName + '/' + componentName + ".module';"; |
| 70 | + utils.addToFile(parentName + '.module.js', moduleImport, utils.IMPORT_MODULE_MARKER, this.destinationRoot() + '/src/app/components/' + parentPath); |
| 71 | + //dependency |
| 72 | + const dependencyImport = "'" + data.componentModule + "',"; |
| 73 | + utils.addToFile(parentName + '.module.js', dependencyImport, utils.IMPORT_DEPENDENCY_MARKER, this.destinationRoot() + '/src/app/components/' + parentPath); |
| 74 | + } catch (err) { |
| 75 | + this.log('Parent component files not found.'); |
| 76 | + return; |
| 77 | + } |
| 78 | + } |
54 | 79 |
|
55 | | - // copy template files, no need for try and catch since file structure already exists from above |
56 | | - this.fs.copyTpl(this.templatePath('_component.html'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component.html'), data); |
57 | | - this.fs.copyTpl(this.templatePath('_component.scss'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component.scss'), data); |
58 | | - this.fs.copyTpl(this.templatePath('_component.module.js'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.module.js'), data); |
59 | | - this.fs.copyTpl(this.templatePath('_component.component.js'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component' + '.js'), data); |
60 | | - this.fs.copyTpl(this.templatePath('_component.component-spec.js'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component-spec' + '.js'), data); |
| 80 | + // checking if the module exists, if so it is a duplicate |
| 81 | + isDuplicate = utils.existsSync(this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.module.js')); |
61 | 82 |
|
62 | | - // Documenting the creation of the component |
63 | | - if(!isDuplicate){ |
64 | | - try{ |
65 | | - const nestedLineMarkExtensionForDocs = " for "+fullPath; |
66 | | - const descriptionForDocs = (this.props.description && this.props.description.length>0)?this.props.description:data.componentName + " component"; |
67 | | - const componentDocJSONString = '{\n\t\t"name": "' + data.componentName + '", "path": "' + this.props.componentName + '",\n\t\t"components": [\n\t\t\t'+utils.COMPONENT_NESTED_MARKER+nestedLineMarkExtensionForDocs+'\n\t\t],\n\t\t"directives": [\n\t\t\t'+utils.DIRECTIVE_NESTED_MARKER+nestedLineMarkExtensionForDocs+'\n\t\t],\n\t\t"services": [\n\t\t\t'+utils.SERVICE_NESTED_MARKER+nestedLineMarkExtensionForDocs+'\n\t\t],\n\t\t"description": "'+ descriptionForDocs + ' component"\n\t\t},' |
68 | | - // extending the nested Line marker with information about the component in order to insert there later |
69 | | - utils.addToFile(utils.DOCS_STORAGE_FILENAME, componentDocJSONString, utils.COMPONENT_MARKER, this.destinationRoot() + utils.DOCS_ASSETS_PATH); |
70 | | - // if the component has a parent, Link it to its parent |
71 | | - if (pathAsArray.length !== 1) { |
72 | | - const nestedLineMarkExtensionOfParent = " for "+parentPath; |
73 | | - // Foreign Key String for component is injected into the parent component |
74 | | - const componentDocForeignKeyJSONString = '{"path": "' + this.props.componentName + '", "name": "' + data.componentName + '"},'; |
75 | | - utils.addToFile(utils.DOCS_STORAGE_FILENAME, componentDocForeignKeyJSONString, utils.COMPONENT_NESTED_MARKER+nestedLineMarkExtensionOfParent, this.destinationRoot() + utils.DOCS_ASSETS_PATH); |
76 | | - } |
77 | | - } catch (err) { |
78 | | - this.log('Could not document this item due to missing documentation file.'); |
79 | | - } |
| 83 | + // copy template files, no need for try and catch since file structure already exists from above |
| 84 | + this.fs.copyTpl(this.templatePath('_component.html'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component.html'), data); |
| 85 | + this.fs.copyTpl(this.templatePath('_component.scss'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component.scss'), data); |
| 86 | + this.fs.copyTpl(this.templatePath('_component.module.js'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.module.js'), data); |
| 87 | + this.fs.copyTpl(this.templatePath('_component.component.js'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component' + '.js'), data); |
| 88 | + this.fs.copyTpl(this.templatePath('_component.component-spec.js'), this.destinationPath(this.destinationRoot() + '/src/app/components/' + fullPath + '/' + data.componentName + '.component-spec' + '.js'), data); |
| 89 | + |
| 90 | + // Documenting the creation of the component |
| 91 | + if (!isDuplicate) { |
| 92 | + const file = this.destinationPath(this.destinationRoot() + utils.DOCS_ASSETS_PATH+'/'+utils.DOCS_STORAGE_FILENAME); |
| 93 | + jsonfile.readFile(file, function(err, docsJSON) { |
| 94 | + if (err) { |
| 95 | + this.log('Could not document this item due to missing or corrupted documentation file.'); |
| 96 | + return; |
80 | 97 | } |
81 | | - }; |
| 98 | + const descriptionForDocs = (this.props.description && this.props.description.length > 0) |
| 99 | + ? this.props.description |
| 100 | + : data.componentName + " component"; |
| 101 | + const componentDocJSON = { |
| 102 | + "name": data.componentName, |
| 103 | + "path": this.props.componentName, |
| 104 | + "components": [], |
| 105 | + "directives": [], |
| 106 | + "services": [], |
| 107 | + "description": descriptionForDocs |
| 108 | + }; |
| 109 | + docsJSON.components.push(componentDocJSON); |
| 110 | + if (pathAsArray.length !== 1) { |
| 111 | + // Foreign Key String for component is injected into the parent component |
| 112 | + const componentDocForeignKeyJSON = { |
| 113 | + "path": this.props.componentName, |
| 114 | + "name": data.componentName |
| 115 | + }; |
| 116 | + docsJSON.components[jsonQuery('components[path=' + pathAsArray.slice(0, -1).join('/') + ']', {data: docsJSON}).key].components.push(componentDocForeignKeyJSON); |
| 117 | + } |
| 118 | + jsonfile.writeFile(file, docsJSON, function(err) {}.bind(this)); |
| 119 | + }.bind(this)); |
| 120 | + } |
| 121 | + }; |
82 | 122 | }; |
0 commit comments