@@ -50,8 +50,17 @@ const initProject = async () => {
5050const initFunction = async () => {
5151 // TODO: Add CI/CD support (ID, name, runtime)
5252 let answers = await inquirer.prompt(questionsInitFunction)
53+ let functionFolder = path.join(process.cwd(), 'functions');
5354
54- if (fs.existsSync(path.join(process.cwd(), 'functions', answers.name))) {
55+ if (!fs.existsSync(functionFolder)) {
56+ fs.mkdirSync(functionFolder, {
57+ recursive: true
58+ });
59+ }
60+
61+ const functionDir = path.join(functionFolder, answers.name);
62+
63+ if (fs.existsSync(functionDir)) {
5564 throw new Error(`( ${answers.name} ) already exists in the current directory. Please choose another name.`);
5665 }
5766
@@ -66,20 +75,53 @@ const initFunction = async () => {
6675 parseOutput: false
6776 })
6877
69- let command = `
70- mkdir -m 777 -p 'functions/${answers.name}' && \
71- cd 'functions/${answers.name}' && \
72- git init && \
73- git remote add -f origin https://github.com/{{ sdk .gitUserName }}/functions-starter && \
74- git config core.sparseCheckout true && \
75- echo '${answers.runtime.id}' >> .git/info/sparse-checkout && \
76- git pull origin main && \
77- rm -rf .git && \
78- mv ${answers.runtime.id}/* . && \
79- rm -rf ${answers.runtime.id}`;
80-
81- // Execute the child process but do not print any std output
82- childProcess.execSync(command, { stdio: 'pipe' });
78+ fs.mkdirSync(functionDir, "777");
79+
80+ let gitInitCommands = "git clone --depth 1 --sparse https://github.com/{{ sdk .gitUserName }}/functions-starter ."; // depth prevents fetching older commits reducing the amount fetched
81+
82+ let gitPullCommands = `git sparse-checkout add ${answers.runtime.id}`;
83+
84+ /* Force use CMD as powershell does not support && */
85+ if (process.platform == 'win32') {
86+ gitInitCommands = 'cmd /c "' + gitInitCommands + '"';
87+ gitPullCommands = 'cmd /c "' + gitPullCommands + '"';
88+ }
89+
90+ /* Execute the child process but do not print any std output */
91+ try {
92+ childProcess.execSync(gitInitCommands, { stdio: 'pipe', cwd: functionDir });
93+ childProcess.execSync(gitPullCommands, { stdio: 'pipe', cwd: functionDir });
94+ } catch (error) {
95+ /* Specialised errors with recommended actions to take */
96+ if (error.message.includes('error: unknown option')) {
97+ throw new Error(`${error.message} \n\nSuggestion: Try updating your git to the latest version, then trying to run this command again.`)
98+ } else if (error.message.includes('is not recognized as an internal or external command,') || error.message.includes('command not found')) {
99+ throw new Error(`${error.message} \n\nSuggestion: It appears that git is not installed, try installing git then trying to run this command again.`)
100+ } else {
101+ throw error;
102+ }
103+ }
104+
105+ fs.rmSync(path.join(functionDir, ".git"), { recursive: true });
106+ const copyRecursiveSync = (src, dest) => {
107+ let exists = fs.existsSync(src);
108+ let stats = exists && fs.statSync(src);
109+ let isDirectory = exists && stats.isDirectory();
110+ if (isDirectory) {
111+ if (!fs.existsSync(dest)) {
112+ fs.mkdirSync(dest);
113+ }
114+
115+ fs.readdirSync(src).forEach(function(childItemName) {
116+ copyRecursiveSync(path.join(src, childItemName), path.join(dest, childItemName));
117+ });
118+ } else {
119+ fs.copyFileSync(src, dest);
120+ }
121+ };
122+ copyRecursiveSync(path.join(functionDir, answers.runtime.id), functionDir);
123+
124+ fs.rmSync(`${functionDir}/${answers.runtime.id}`, { recursive: true, force: true });
83125
84126 const readmePath = path.join(process.cwd(), 'functions', answers.name, 'README.md');
85127 const readmeFile = fs.readFileSync(readmePath).toString();
0 commit comments