1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const fs = require('fs'); 17const program = require('commander'); 18 19program.parse(process.argv); 20let name:string = 'HelloAce'; 21let appID:string = 'ace.helloworld'; 22let appName:string = 'HelloAce'; 23if (program.args && program.args[0]) { 24 name = program.args[0]; 25 appID = program.args[0]; 26 appName = program.args[0]; 27} 28 29const regPath: RegExp = /[`~!@#$%^&*()_+<>?:"{},./;'[\]]/im; 30 31/* 32 * Create sample project and files. 33 * @param dist {String} 34 */ 35function createProject(dist: string) { 36 const dist_ = dist.trim().split('/'); 37 if (dist_.length > 1 || regPath.test(dist)) { 38 return console.error( 39 'ERROR: The project name cannot be a path nor contain any special symbol.\n' + 40 "NOTE: To create the template project, run 'npm run create' in the root directory.\n" + 41 "NOTE: To customize the project name, run 'npm run create <projectName>'."); 42 } 43 const appPath:string = dist + '/app.ets'; 44 const manifestPath:string = dist + '/manifest.json'; 45 const indexPath:string = dist + '/pages/index.ets'; 46 47 const app:string = `export default { 48 onCreate() { 49 console.info('Application onCreate') 50 }, 51 onDestroy() { 52 console.info('Application onDestroy') 53 }, 54}`; 55 56 const manifest:string = `{ 57 "appID": "com.example.` + appID + `", 58 "appName": "` + appName + `", 59 "versionName": "1.0.0", 60 "versionCode": 1, 61 "minPlatformVersion": "1.0.1", 62 "pages": [ 63 "pages/index" 64 ], 65 "window": { 66 "designWidth": 750, 67 "autoDesignWidth": false 68 } 69}`; 70 71 const index:string = `@Entry 72@Component 73struct MyComponent { 74 private value1: string = "hello world 1"; 75 private value2: string = "hello world 2"; 76 private value3: string = "hello world 3"; 77 78 build() { 79 Column() { 80 Text(this.value1); 81 Text(this.value2); 82 Text(this.value3); 83 } 84 } 85}`; 86 87 fs.mkdir(dist + '/pages', { recursive: true }, (err) => { 88 if (err) { 89 return console.error('ERROR: Failed to create project directory.'); 90 } 91 fs.writeFile(appPath, app, (err) => { 92 if (err) { 93 return console.error('ERROR: Failed to write app.ets.'); 94 } 95 }); 96 fs.writeFile(manifestPath, manifest, (err) => { 97 if (err) { 98 return console.error('ERROR: Failed to write manifest.json.'); 99 } 100 }); 101 fs.writeFile(indexPath, index, (err) => { 102 if (err) { 103 return console.error('ERROR: Failed to write index.ets.'); 104 } 105 }); 106 }); 107} 108 109createProject(name); 110