1
1
const Tail = require('tail').Tail;
2
+ const { parse: parseDotenv } = require('dotenv');
2
3
const chalk = require('chalk');
3
4
const ignore = require("ignore");
4
5
const tar = require("tar");
@@ -80,6 +81,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
80
81
entrypoint: func.entrypoint,
81
82
path: func.path,
82
83
commands: func.commands,
84
+ scopes: func.scopes ?? []
83
85
};
84
86
85
87
drawTable([settings]);
@@ -110,7 +112,9 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
110
112
fs.writeFileSync(errorsPath, '');
111
113
}
112
114
115
+ const userVariables = {};
113
116
const variables = {};
117
+
114
118
if(!noVariables) {
115
119
try {
116
120
const { variables: remoteVariables } = await paginate(functionsListVariables, {
@@ -120,12 +124,24 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
120
124
121
125
remoteVariables.forEach((v) => {
122
126
variables[v.key] = v.value;
127
+ userVariables[v.key] = v.value;
123
128
});
124
129
} catch(err) {
125
130
warn("Remote variables not fetched. Production environment variables will not be avaiable. Reason: " + err.message);
126
131
}
127
132
}
128
133
134
+ const functionPath = path.join(process.cwd(), func.path);
135
+ const envPath = path.join(functionPath, '.env');
136
+ if(fs.existsSync(envPath)) {
137
+ const env = parseDotenv(fs.readFileSync(envPath).toString() ?? '');
138
+
139
+ Object.keys(env).forEach((key) => {
140
+ variables[key] = env[key];
141
+ userVariables[key] = env[key];
142
+ });
143
+ }
144
+
129
145
variables['APPWRITE_FUNCTION_API_ENDPOINT'] = globalConfig.getFrom('endpoint');
130
146
variables['APPWRITE_FUNCTION_ID'] = func.$id;
131
147
variables['APPWRITE_FUNCTION_NAME'] = func.name;
@@ -148,6 +164,13 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
148
164
headers['x-appwrite-user-jwt'] = JwtManager.userJwt ?? '';
149
165
variables['OPEN_RUNTIMES_HEADERS'] = JSON.stringify(headers);
150
166
167
+ if(Object.keys(userVariables).length > 0) {
168
+ drawTable(Object.keys(userVariables).map((key) => ({
169
+ key,
170
+ value: userVariables[key].split("").filter((_, i) => i < 16).map(() => "*").join("")
171
+ })));
172
+ }
173
+
151
174
await dockerPull(func);
152
175
153
176
new Tail(logsPath).on("line", function(data) {
@@ -158,10 +181,27 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
158
181
});
159
182
160
183
if(!noReload) {
184
+ const ignorer = ignore();
185
+ ignorer.add('.appwrite');
186
+ ignorer.add('code.tar.gz');
187
+
188
+ if (func.ignore) {
189
+ ignorer.add(func.ignore);
190
+ } else if (fs.existsSync(path.join(functionPath, '.gitignore'))) {
191
+ ignorer.add(fs.readFileSync(path.join(functionPath, '.gitignore')).toString());
192
+ }
193
+
161
194
chokidar.watch('.', {
162
195
cwd: path.join(process.cwd(), func.path),
163
196
ignoreInitial: true,
164
- ignored: [ ...(func.ignore ?? []), 'code.tar.gz', '.appwrite', '.appwrite/', '.appwrite/*', '.appwrite/**', '.appwrite/*.*', '.appwrite/**/*.*' ]
197
+ ignored: (xpath) => {
198
+ const relativePath = path.relative(functionPath, xpath);
199
+
200
+ if(!relativePath) {
201
+ return false;
202
+ }
203
+ return ignorer.ignores(relativePath);
204
+ }
165
205
}).on('all', async (_event, filePath) => {
166
206
Queue.push(filePath);
167
207
});
@@ -187,7 +227,6 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
187
227
} else {
188
228
log('Hot-swapping function.. Files with change are ' + files.join(', '));
189
229
190
- const functionPath = path.join(process.cwd(), func.path);
191
230
const hotSwapPath = path.join(functionPath, '.appwrite/hot-swap');
192
231
const buildPath = path.join(functionPath, '.appwrite/build.tar.gz');
193
232
@@ -201,6 +240,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
201
240
202
241
await tar
203
242
.extract({
243
+ keep: true,
204
244
gzip: true,
205
245
sync: true,
206
246
cwd: hotSwapPath,
@@ -211,6 +251,8 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
211
251
ignorer.add('.appwrite');
212
252
if (func.ignore) {
213
253
ignorer.add(func.ignore);
254
+ } else if (fs.existsSync(path.join(functionPath, '.gitignore'))) {
255
+ ignorer.add(fs.readFileSync(path.join(functionPath, '.gitignore')).toString());
214
256
}
215
257
216
258
const filesToCopy = getAllFiles(functionPath).map((file) => path.relative(functionPath, file)).filter((file) => !ignorer.ignores(file));
@@ -237,8 +279,6 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
237
279
file: buildPath
238
280
}, ['.']);
239
281
240
- fs.rmSync(hotSwapPath, { recursive: true, force: true });
241
-
242
282
await dockerStart(func, variables, port);
243
283
}
244
284
} catch(err) {
0 commit comments