Skip to content

Commit d03dbaf

Browse files
Handle converting unix commands to windows ones
1 parent 1d5293d commit d03dbaf

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,54 @@ const initFunction = async () => {
7171
parseOutput: false
7272
})
7373

74-
let command = `
75-
mkdir -m 777 -p 'functions/${answers.name}' && \
76-
cd 'functions/${answers.name}' && \
74+
let initFirstCommand = `
75+
cd '${answers.name}' && \
7776
git init && \
7877
git remote add -f origin https://github.com/{{ sdk.gitUserName }}/functions-starter && \
79-
git config core.sparseCheckout true && \
80-
echo '${answers.runtime.id}' >> .git/info/sparse-checkout && \
81-
git pull origin main && \
82-
rm -rf .git && \
83-
mv ${answers.runtime.id}/* . && \
84-
rm -rf ${answers.runtime.id}`;
78+
git config core.sparseCheckout true`;
79+
80+
let initSecondCommand = `
81+
cd '${answers.name}' && \
82+
git pull origin dev && \
83+
git checkout dev`;
84+
85+
// Convert commands from unix based ones to windows based ones
86+
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 + '"';
91+
}
92+
93+
fs.mkdirSync(answers.name, { recursive: true });
94+
fs.chmodSync(answers.name, 0o777);
8595

8696
// Execute the child process but do not print any std output
87-
childProcess.execSync(command, { stdio: 'pipe' });
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 });
109+
}
110+
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+
fs.rmSync(`./${answers.name}/${answers.runtime.id}`, { recursive: true, force: true });
88122

89123
const readmePath = path.join(process.cwd(), 'functions', answers.name, 'README.md');
90124
const readmeFile = fs.readFileSync(readmePath).toString();

0 commit comments

Comments
 (0)