Skip to content

Commit

Permalink
feat: add 'list' command
Browse files Browse the repository at this point in the history
  • Loading branch information
Build Pipeline committed Jan 20, 2023
1 parent d1c15a2 commit f7c68b0
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 46 deletions.
27 changes: 21 additions & 6 deletions bin/osts-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import minimist from "minimist";
import { exit } from "process";
import { pack } from './osts-pack.js';
import { unpack } from './osts-unpack.js';
import { copyOfficeScriptSimplifiedDeclaration } from "./osts-utils.js";
import { askForPreferences, copyOfficeScriptSimplifiedDeclaration, listOfficeScript, savePreferences } from "./osts-utils.js";
const DEFAULT_PATH = 'osts';
function help() {
console.log(`
Usage:
========
osts list --path // list OSTS packages in SPO path.
osts unpack [--path, -p <dest dir> [--dts] ] // download OSTS package and extract body (.ts) to dest dir (default '${DEFAULT_PATH}').
// If --dts is specified an Office Script Simplified TS Declaration file will be copied in dest dir
Expand All @@ -25,23 +26,37 @@ osts dts [--path, -p <dest dir>] // an Office Script Simplified TS Declaration f
boolean: 'dts',
alias: { 'p': 'path' },
'default': { 'path': DEFAULT_PATH },
unknown: (args) => args.toLowerCase() === 'pack' || args.toLowerCase() === 'unpack' || args.toLowerCase() === 'dts'
unknown: (args) => /pack|unpack|list|dts/i.test(args)
});
// console.log( cli );
// Check Arguments
const _cmd = () => (cli._.length > 0) ? cli._[0].toLowerCase() : undefined;
const _path = () => cli['path']?.length === 0 ? DEFAULT_PATH : cli.path;
try {
// console.log( 'path', cli['path'], _path() )
if (_cmd() === 'pack') {
const command = _cmd();
// console.log( 'command', command, 'path', cli['path'], _path() )
if (command === 'list') {
const prefs = await askForPreferences();
if (!prefs)
exit(-1);
const result = await listOfficeScript(prefs);
console.table(result.map(file => ({
Name: file.Name,
Length: `${file.Length} bytes`,
RelativeUrl: path.dirname(path.relative(prefs.toPath(), file.ServerRelativeUrl))
})));
savePreferences(prefs);
exit(0);
}
else if (command === 'pack') {
const code = await pack(_path());
exit(code);
}
else if (_cmd() === 'unpack') {
else if (command === 'unpack') {
const code = await unpack(_path(), cli['dts']);
exit(code);
}
else if (_cmd() === 'dts') {
else if (command === 'dts') {
const code = await copyOfficeScriptSimplifiedDeclaration(_path());
exit(code);
}
Expand Down
6 changes: 2 additions & 4 deletions bin/osts-unpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'zx/globals';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import { chooseFile, loadOSTS, askForPreferences, savePreferences, copyOfficeScriptSimplifiedDeclaration as CP_D_TS } from './osts-utils.js';
import { chooseFile, loadOSTS, askForPreferences, savePreferences, copyOfficeScriptSimplifiedDeclaration as CP_D_TS, listOfficeScript } from './osts-utils.js';
const fsWriteFile = promisify(fs.writeFile);
const fsMkdir = promisify(fs.mkdir);
async function extractBody(file, bodyDirPath) {
Expand All @@ -20,9 +20,7 @@ export async function unpack(bodyDirPath, copyOfficeScriptSimplifiedDeclaration)
return 0;
try {
$.verbose = false;
const JMESPathQuery = `[?ends_with(Name, '.osts')]`;
const result = await $ `m365 spo file list --webUrl ${prefs.weburl} --folder ${prefs.folder} --recursive --query ${JMESPathQuery}`;
const spoFileListResult = JSON.parse(result.stdout);
const spoFileListResult = await listOfficeScript(prefs);
if (!spoFileListResult.length || spoFileListResult.length === 0) {
console.error(`no OSTS files detected at folder '${prefs.folder}`);
return -1;
Expand Down
17 changes: 16 additions & 1 deletion bin/osts-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export const askForPreferences = async () => {
const candidateFolder = await askForFolder(_prefs);
if (!candidateFolder)
return;
return { weburl: candidateWebUrl, folder: candidateFolder };
return {
weburl: candidateWebUrl,
folder: candidateFolder,
toPath: () => path.join(new URL(candidateWebUrl).pathname, candidateFolder)
};
};
export const savePreferences = (data) => {
_prefs.weburl = data.weburl;
Expand Down Expand Up @@ -91,3 +95,14 @@ export async function copyOfficeScriptSimplifiedDeclaration(bodyDirPath) {
return -1;
}
}
export async function listOfficeScript(prefs) {
const list_parameters = [
'--webUrl', prefs.weburl,
'--folder', prefs.folder,
'--query', "[?ends_with(Name, '.osts')]",
'--recursive'
];
const result = await $ `m365 spo file list ${list_parameters}`.quiet();
const spoFileListResult = JSON.parse(result.stdout);
return spoFileListResult;
}
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bsorrentino/osts-cli",
"version": "1.0.3",
"version": "1.1.0",
"description": "CLI for manage OSTS files",
"types": "office-js-simplified.d.ts",
"files": [
Expand All @@ -23,6 +23,7 @@
],
"license": "MIT",
"scripts": {
"start": "node index.mjs",
"build": "tsc",
"pack": "node index.mjs pack",
"unpack": "node index.mjs unpack",
Expand All @@ -31,12 +32,12 @@
},
"devDependencies": {
"@types/minimist": "^1.2.2",
"@types/node": "^16.11.5",
"@types/office-js": "^1.0.294",
"@types/node": "^16.18.11",
"@types/office-js": "^1.0.311",
"typescript": "^4.7.4"
},
"engines": {
"node": ">=14.0.0"
"node": ">=16.0.0"
},
"dependencies": {
"minimist": "^1.2.6",
Expand Down
31 changes: 25 additions & 6 deletions src/osts-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import minimist, {ParsedArgs } from "minimist";
import { exit } from "process";
import { pack } from './osts-pack.js'
import { unpack } from './osts-unpack.js'
import { copyOfficeScriptSimplifiedDeclaration } from "./osts-utils.js";
import { askForPreferences, copyOfficeScriptSimplifiedDeclaration, listOfficeScript, savePreferences } from "./osts-utils.js";

const DEFAULT_PATH = 'osts'

Expand All @@ -12,6 +12,7 @@ function help() {
console.log(`
Usage:
========
osts list --path // list OSTS packages in SPO path.
osts unpack [--path, -p <dest dir> [--dts] ] // download OSTS package and extract body (.ts) to dest dir (default '${DEFAULT_PATH}').
// If --dts is specified an Office Script Simplified TS Declaration file will be copied in dest dir
Expand All @@ -31,7 +32,7 @@ osts dts [--path, -p <dest dir>] // an Office Script Simplified TS Declaration f
boolean: 'dts',
alias: { 'p': 'path'},
'default': { 'path' : DEFAULT_PATH},
unknown: (args: string) => args.toLowerCase()==='pack' || args.toLowerCase()==='unpack' || args.toLowerCase()==='dts'
unknown: (args: string) => /pack|unpack|list|dts/i.test(args)
})

// console.log( cli );
Expand All @@ -45,17 +46,35 @@ osts dts [--path, -p <dest dir>] // an Office Script Simplified TS Declaration f

try {

// console.log( 'path', cli['path'], _path() )
const command = _cmd()

if( _cmd() === 'pack') {
// console.log( 'command', command, 'path', cli['path'], _path() )

if( command === 'list' ) {
const prefs = await askForPreferences()
if( !prefs ) exit(-1)

const result = await listOfficeScript( prefs )

console.table( result.map( file => ({
Name: file.Name,
Length: `${file.Length} bytes`,
RelativeUrl: path.dirname(path.relative( prefs.toPath(), file.ServerRelativeUrl ))
})) )

savePreferences( prefs )

exit(0)
}
else if( command === 'pack') {
const code = await pack( _path() )
exit(code)
}
else if( _cmd() === 'unpack' ) {
else if( command === 'unpack' ) {
const code = await unpack( _path(), cli['dts'] )
exit(code)
}
else if( _cmd() === 'dts' ) {
else if( command === 'dts' ) {
const code = await copyOfficeScriptSimplifiedDeclaration( _path() )
exit(code)
}
Expand Down
13 changes: 8 additions & 5 deletions src/osts-unpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import {
askForPreferences,
savePreferences,
SPOFile,
copyOfficeScriptSimplifiedDeclaration as CP_D_TS } from './osts-utils.js'
copyOfficeScriptSimplifiedDeclaration as CP_D_TS,
listOfficeScript} from './osts-utils.js'

export interface Options {
copyOfficeScriptSimplifiedDeclaration?:boolean

}

const fsWriteFile = promisify(fs.writeFile)
const fsMkdir = promisify(fs.mkdir)
Expand All @@ -35,10 +41,7 @@ export async function unpack( bodyDirPath:string, copyOfficeScriptSimplifiedDecl

$.verbose = false

const JMESPathQuery = `[?ends_with(Name, '.osts')]`
const result = await $`m365 spo file list --webUrl ${prefs.weburl} --folder ${prefs.folder} --recursive --query ${JMESPathQuery}`

const spoFileListResult = JSON.parse( result.stdout ) as Array<SPOFile>
const spoFileListResult = await listOfficeScript( prefs )

if( !spoFileListResult.length || spoFileListResult.length === 0 ) {
console.error( `no OSTS files detected at folder '${prefs.folder}` )
Expand Down
34 changes: 31 additions & 3 deletions src/osts-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ export type LoadedOSTS = OSTS & { bodyFilePath:string }
export interface PreferenceData {
weburl:string,
folder:string
toPath:() => string
}

export interface SPOFile {
Name: string,
ServerRelativeUrl: string,
Name: string
ServerRelativeUrl: string
UniqueId:string
Length:number
Level:number
Exists:boolean
MajorVersion:number
MinorVersion:number
TimeCreated:Date
TimeLastModified:Date
}

const askForWebUrl = async (prefs:Partial<PreferenceData>) => {
Expand Down Expand Up @@ -79,7 +87,11 @@ export const askForPreferences = async ():Promise<PreferenceData|undefined> => {
const candidateFolder = await askForFolder( _prefs )
if( !candidateFolder ) return

return { weburl:candidateWebUrl, folder:candidateFolder }
return {
weburl:candidateWebUrl,
folder:candidateFolder,
toPath: () => path.join( new URL( candidateWebUrl ).pathname, candidateFolder)
}

}

Expand Down Expand Up @@ -155,3 +167,19 @@ export async function copyOfficeScriptSimplifiedDeclaration( bodyDirPath:string
return -1
}
}


export async function listOfficeScript( prefs: PreferenceData ): Promise<Array<SPOFile>> {

const list_parameters = [
'--webUrl', prefs.weburl,
'--folder', prefs.folder,
'--query', "[?ends_with(Name, '.osts')]",
'--recursive'
]
const result = await $`m365 spo file list ${list_parameters}`.quiet()

const spoFileListResult = JSON.parse( result.stdout ) as Array<SPOFile>

return spoFileListResult
}

0 comments on commit f7c68b0

Please sign in to comment.