@@ -54,9 +54,17 @@ const initProject = async () => {
54
54
}
55
55
56
56
const initFunction = async () => {
57
- let answers = await inquirer.prompt(questionsInitFunction)
57
+ let answers = await inquirer.prompt(questionsInitFunction);
58
58
59
- if (fs.existsSync(path.join(process.cwd(), 'functions', answers.name))) {
59
+ if (!fs.existsSync(path.join(process.cwd(), 'functions'))) {
60
+ fs.mkdirSync(path.join(process.cwd(), 'functions'), {
61
+ recursive: true
62
+ });
63
+ }
64
+
65
+ const functionDir = path.join(process.cwd(), 'functions', answers.name);
66
+
67
+ if (fs.existsSync(functionDir)) {
60
68
throw new Error(`( ${answers.name} ) already exists in the current directory. Please choose another name.`);
61
69
}
62
70
@@ -71,53 +79,47 @@ const initFunction = async () => {
71
79
parseOutput: false
72
80
})
73
81
74
- let initFirstCommand = `
75
- cd '${answers.name}' && \
76
- git init && \
77
- git remote add -f origin https://github.com/{{ sdk .gitUserName }}/functions-starter && \
78
- git config core.sparseCheckout true`;
82
+ fs.mkdirSync(functionDir, "777");
79
83
80
- let initSecondCommand = `
81
- cd '${answers.name}' && \
82
- git pull origin dev && \
83
- git checkout dev `;
84
+ const gitInitCommands =
85
+ `git init && ` +
86
+ ` git remote add -f origin https://github.com/{{ sdk . gitUserName }}/functions-starter && ` +
87
+ ` git config core.sparseCheckout true `;
84
88
85
- // Convert commands from unix based ones to windows based ones
89
+ const gitPullCommands = `git pull origin main`;
90
+
91
+ // Force use CMD as powershell does not support &&
86
92
if (process.platform == 'win32') {
87
- initFirstCommand = initFirstCommand.replace(/'/g, '"').replace(/\\/g, '').replace(/\n/g, '').replace(/ +/g, ' ');
88
- initFirstCommand = 'cmd /c "' + initFirstCommand + '"';
89
- initSecondCommand = initSecondCommand.replace(/'/g, '"').replace(/\\/g, '').replace(/\n/g, '').replace(/ +/g, ' ');
90
- initSecondCommand = 'cmd /c "' + initSecondCommand + '"';
93
+ gitInitCommands = 'cmd /c "' + gitInitCommands + '"';
94
+ gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
91
95
}
92
96
93
- fs.mkdirSync(answers.name, { recursive: true });
94
- fs.chmodSync(answers.name, 0o777);
95
-
96
97
// Execute the child process but do not print any std output
97
- childProcess.execSync(initFirstCommand, { stdio: 'pipe' });
98
-
99
- fs.writeFileSync(`${answers.name}/.git/info/sparse-checkout`, answers.runtime.id);
100
-
101
- childProcess.execSync(initSecondCommand, { stdio: 'pipe' });
102
-
103
- fs.rmSync(`${answers.name}/.git`, { recursive: true, force: true });
104
-
105
- // Copy files
106
- function copyFolderSync(from, to) {
107
- if (!fs.existsSync(to)) {
108
- fs.mkdirSync(to, { recursive: true });
98
+ childProcess.execSync(gitInitCommands, { stdio: 'pipe' });
99
+
100
+ fs.writeFileSync(path.join(functionDir, ".git/info/sparse-checkout"), `${answers.runtime.id}`);
101
+
102
+ childProcess.execSync(gitPullCommands, { stdio: 'pipe' });
103
+
104
+ fs.rmSync(path.join(functionDir, ".git"), { recursive: true });
105
+ const copyRecursiveSync = (src, dest) => {
106
+ let exists = fs.existsSync(src);
107
+ let stats = exists && fs.statSync(src);
108
+ let isDirectory = exists && stats.isDirectory();
109
+ if (isDirectory) {
110
+ if (!fs.existsSync(dest)) {
111
+ fs.mkdirSync(dest);
112
+ }
113
+
114
+ fs.readdirSync(src).forEach(function(childItemName) {
115
+ copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
116
+ });
117
+ } else {
118
+ fs.copyFileSync(src, dest);
109
119
}
120
+ };
121
+ copyRecursiveSync(path.join(functionDir, answers.runtime.id), functionDir);
110
122
111
- fs.readdirSync(from).forEach(element => {
112
- if (fs.lstatSync(path.join(from, element)).isFile()) {
113
- fs.copyFileSync(path.join(from, element), path.join(to, element));
114
- } else {
115
- copyFolderSync(path.join(from, element), path.join(to, element));
116
- }
117
- });
118
- }
119
-
120
- copyFolderSync(`./${answers.name}/${answers.runtime.id}`, `./${answers.name}/`);
121
123
fs.rmSync(`./${answers.name}/${answers.runtime.id}`, { recursive: true, force: true });
122
124
123
125
const readmePath = path.join(process.cwd(), 'functions', answers.name, 'README.md');
0 commit comments