Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
.then(async (token: string) => {
const project = await getProject(token);
showWarning(project);
const confirmation = await userConfirmation();
if (!confirmation) {
console.log('\nYou chose not to continue. Nothing was changed.\n');
return false;
}
console.log('');
let task = showTask('Downloading project configuration');
const config = await firebaseTools.setup.web({ project, token });
task.done('config/project.json');
// Write config to top-level config directory
await promisify(writeFile)(
resolvePath(__dirname, '../../config/project.json'),
JSON.stringify(config, null, 2)
);
// Deploy database rules
task = showTask('Deploying Firestore indexes and security rules');
await firebaseTools.deploy({
project,
token,
cwd: resolvePath(__dirname, '../../config')
});
await firebaseTools.deploy({
project,
token,
cwd: resolvePath(__dirname, '../../config')
});
// Firestore calls grpc.load() which has been deprecated and we
// get an ugly warning on screen. This mutes it temporarily.
const unmute = muteDeprecationWarning();
const firestore = firebase.initializeApp(config).firestore();
firestore.settings({ timestampsInSnapshots: true });
const rootRef = firestore.doc('/');
task = showTask('Deleting "shops" collection');
await firebaseTools.firestore.delete('/shops', {
project,
yes: true,
recursive: true
});
task = showTask('Loading test data into "shops" collection');
await loadTestDataset(rootRef, loadJSONFile(
'./data.json'
) as TestCollection[]);
unmute();
task.done();
return;
})
.then((result?: boolean) => {
const config = require(resolvePath(
homedir(),
'.config/configstore/firebase-tools.json'
));
cachedToken = config.tokens.refresh_token;
} catch (err) {
/* no problem */
}
if (cachedToken) {
return cachedToken;
}
const {
tokens: { refresh_token: freshToken }
} = await firebaseTools.login.ci();
return freshToken;
}
task('deploy:devapp', ['stage-deploy:devapp'], () => {
return firebaseTools.deploy({cwd: projectDir, only: 'hosting'})
// Firebase tools opens a persistent websocket connection and the process will never exit.
.then(() => {
console.log('Successfully deployed the dev-app to firebase');
process.exit(0);
})
.catch((err: any) => {
console.log(err);
process.exit(1);
});
});
task('deploy:devapp', ['stage-deploy:devapp'], () => {
return firebaseTools.deploy({cwd: projectDir, only: 'hosting'})
// Firebase tools opens a persistent websocket connection and the process will never exit.
.then(() => { console.log('Successfully deployed the demo-app to firebase'); process.exit(0); })
.catch((err: any) => { console.log(err); process.exit(1); });
});
task('deploy:devapp', ['stage-deploy:devapp'], () => {
return (
firebaseTools
.deploy({ cwd: projectDir, only: 'hosting' })
// Firebase tools opens a persistent websocket connection and the process will never exit.
.then(() => {
console.log('Successfully deployed the demo-app to firebase');
process.exit(0);
})
.catch((err: any) => {
console.log(err);
process.exit(1);
})
);
});
task('deploy:devapp', ['stage-deploy:devapp'], () => {
return firebaseTools.deploy({cwd: projectDir, only: 'hosting'})
// Firebase tools opens a persistent websocket connection and the process will never exit.
.then(() => { console.log('Successfully deployed the demo-app to firebase'); process.exit(0); })
.catch((err: any) => { console.log(err); process.exit(1); });
});
if (!(context.auth && context.auth.token && context.auth.token.admin)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
// Run a recursive delete on the given document or collection path.
// The 'token' must be set in the functions config, and can be generated
// at the command line by running 'firebase login:ci'.
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
.then(() => {
return {
path: path
};
});
});
// [END recursive_delete_function]
async function deployConfig(configFile: string, project: string) {
console.log(`Deploying ${configFile} to ${project}.`);
// Read the local JSON file and then wrap it in { runtime: config: { ... } }
const configFileString = fs.readFileSync(configFile).toString();
const config = {
runtime: {
config: JSON.parse(configFileString)
}
};
// Encode the proposed config into a flat map of dot-separated values
const newConfig = encoding.flattenConfig(config, encoding.Direction.ENCODE);
// Get the current runtime config from Firebase as a giant object
const current = await firebase.functions.config.get("runtime", {
project: project
});
// Decode the config into a flat map of dot-separated values.
const currentConfig = encoding.flattenConfig(
{
runtime: current
},
encoding.Direction.NONE
);
const keysRemoved: string[] = [];
const keysAddedOrChanged: string[] = [];
const newKeys = Object.keys(newConfig);
const currentKeys = Object.keys(currentConfig);
const val = newConfig[key];
args.push(`${key}=${val}`);
}
}
// If no changes, we're done
if (args.length == 0) {
console.log("No config changes.");
return;
}
// Log out everything that is changing
console.log(args);
// Set the new config
await firebase.functions.config.set(args, {
project: project
});
}