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,28 @@ 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('.DS_Store');
187
+ ignorer.add('code.tar.gz');
188
+
189
+ if (func.ignore) {
190
+ ignorer.add(func.ignore);
191
+ } else if (fs.existsSync(path.join(functionPath, '.gitignore'))) {
192
+ ignorer.add(fs.readFileSync(path.join(functionPath, '.gitignore')).toString());
193
+ }
194
+
161
195
chokidar.watch('.', {
162
196
cwd: path.join(process.cwd(), func.path),
163
197
ignoreInitial: true,
164
- ignored: [ ...(func.ignore ?? []), 'code.tar.gz', '.appwrite', '.appwrite/', '.appwrite/*', '.appwrite/**', '.appwrite/*.*', '.appwrite/**/*.*' ]
198
+ ignored: (xpath) => {
199
+ const relativePath = path.relative(functionPath, xpath);
200
+
201
+ if(!relativePath) {
202
+ return false;
203
+ }
204
+ return ignorer.ignores(relativePath);
205
+ }
165
206
}).on('all', async (_event, filePath) => {
166
207
Queue.push(filePath);
167
208
});
@@ -187,7 +228,6 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
187
228
} else {
188
229
log('Hot-swapping function.. Files with change are ' + files.join(', '));
189
230
190
- const functionPath = path.join(process.cwd(), func.path);
191
231
const hotSwapPath = path.join(functionPath, '.appwrite/hot-swap');
192
232
const buildPath = path.join(functionPath, '.appwrite/build.tar.gz');
193
233
@@ -201,6 +241,7 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
201
241
202
242
await tar
203
243
.extract({
244
+ keep: true,
204
245
gzip: true,
205
246
sync: true,
206
247
cwd: hotSwapPath,
@@ -209,8 +250,11 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
209
250
210
251
const ignorer = ignore();
211
252
ignorer.add('.appwrite');
253
+ ignorer.add('.DS_Store');
212
254
if (func.ignore) {
213
255
ignorer.add(func.ignore);
256
+ } else if (fs.existsSync(path.join(functionPath, '.gitignore'))) {
257
+ ignorer.add(fs.readFileSync(path.join(functionPath, '.gitignore')).toString());
214
258
}
215
259
216
260
const filesToCopy = getAllFiles(functionPath).map((file) => path.relative(functionPath, file)).filter((file) => !ignorer.ignores(file));
@@ -237,8 +281,6 @@ const runFunction = async ({ port, functionId, noVariables, noReload, userId } =
237
281
file: buildPath
238
282
}, ['.']);
239
283
240
- fs.rmSync(hotSwapPath, { recursive: true, force: true });
241
-
242
284
await dockerStart(func, variables, port);
243
285
}
244
286
} catch(err) {
0 commit comments