Skip to content

Instantly share code, notes, and snippets.

@benhughes
Last active August 31, 2019 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhughes/7d3d0bed8dfeef6f894496e1caaa44fb to your computer and use it in GitHub Desktop.
Save benhughes/7d3d0bed8dfeef6f894496e1caaa44fb to your computer and use it in GitHub Desktop.
Changing default to true
const {prettier, parserBabylon} = importModule('./prettier-scriptable');
const prettierConfig = {
singleQuote: true,
trailingComma: 'es5',
bracketSpacing: false,
parser: 'babel',
plugins: [parserBabylon],
};
const fm = FileManager.iCloud();
const docsDir = fm.documentsDirectory();
const scriptNames = getScriptNames();
// change to false if you would like to run this on every file in you scriptable directory
const askForSctriptName = true;
const scripts = askForSctriptName ? await pickScriptName() : scriptNames;
scripts.forEach(script => {
const scriptContent = getScriptContent(script);
const newScriptContent = prettier.format(scriptContent, prettierConfig);
if (scriptContent !== newScriptContent) {
overwriteScript(script, newScriptContent);
console.log(`updated ${script}`);
}
});
async function pickScriptName() {
const alert = new Alert();
alert.title = 'Select Script';
alert.message =
'The script can be run by force touching or long pressing on the notification.';
alert.addAction('all');
scriptNames.forEach(scriptName => alert.addAction(scriptName));
alert.addCancelAction('Cancel');
const idx = await alert.presentAlert();
if (idx == -1) {
return [];
} else if (idx === 0) {
return scriptNames;
} else {
return [scriptNames[idx - 1]];
}
}
function getScriptNames() {
return fm
.listContents(docsDir)
.filter(f => {
return f.endsWith('.js');
})
.sort();
}
function getScriptContent(scriptName) {
return fm.readString(`${docsDir}/${scriptName}`);
}
function overwriteScript(scriptName, newContent) {
return fm.writeString(`${docsDir}/${scriptName}`, newContent);
}
Script.complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment