Skip to content

Commit 0810b84

Browse files
Merge Matej's Changes into my changes
1 parent d03dbaf commit 0810b84

File tree

1 file changed

+43
-41
lines changed

1 file changed

+43
-41
lines changed

templates/cli/lib/commands/init.js.twig

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ const initProject = async () => {
5454
}
5555

5656
const initFunction = async () => {
57-
let answers = await inquirer.prompt(questionsInitFunction)
57+
let answers = await inquirer.prompt(questionsInitFunction);
5858

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)) {
6068
throw new Error(`( ${answers.name} ) already exists in the current directory. Please choose another name.`);
6169
}
6270

@@ -71,53 +79,47 @@ const initFunction = async () => {
7179
parseOutput: false
7280
})
7381

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");
7983

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`;
8488

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 &&
8692
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 + '"';
9195
}
9296

93-
fs.mkdirSync(answers.name, { recursive: true });
94-
fs.chmodSync(answers.name, 0o777);
95-
9697
// 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);
109119
}
120+
};
121+
copyRecursiveSync(path.join(functionDir, answers.runtime.id), functionDir);
110122

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}/`);
121123
fs.rmSync(`./${answers.name}/${answers.runtime.id}`, { recursive: true, force: true });
122124

123125
const readmePath = path.join(process.cwd(), 'functions', answers.name, 'README.md');

0 commit comments

Comments
 (0)