Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodecleyre committed Sep 1, 2023
1 parent 515b009 commit 511394d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
9 changes: 6 additions & 3 deletions docs/docs/cmd/spo/listitem/listitem-batch-add.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ m365 spo listitem batch add [options]
## Options

```md definition-list
`-p, --filePath <filePath>`
: The absolute or relative path to a flat file containing the list items.

`-u, --webUrl <webUrl>`
: URL of the site.

`-p, --filePath [filePath]`
: The absolute or relative path to a flat file containing the list items.

`-c, --csvContent [csvContent]`
: A string content in CSV-format containing the list items. Specify filePath or csvContent, but not both.

`-l, --listId [listId]`
: ID of the list. Specify either `listTitle`, `listId` or `listUrl`, but not multiple.

Expand Down
11 changes: 11 additions & 0 deletions src/m365/spo/commands/listitem/listitem-batch-add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ describe(commands.LISTITEM_BATCH_ADD, () => {
await command.action(logger, { options: { webUrl: webUrl, filePath: filePath, listId: listId, verbose: true } } as any);
});

it('adds items in batch to a sharepoint list retrieved by id with csv content', async () => {
sinon.stub(request, 'post').callsFake(async (opts: any) => {
if (opts.url === `${webUrl}/_api/$batch`) {
return Promise.resolve(mockBatchSuccessfulResponse);
}
throw 'Invalid request';
});

await command.action(logger, { options: { webUrl: webUrl, csvContent: csvContent, listId: listId, verbose: true } } as any);
});

it('adds items in batch to a sharepoint list retrieved by title', async () => {
sinon.stub(fs, 'readFileSync').callsFake(_ => csvContent);
sinon.stub(request, 'post').callsFake(async (opts: any) => {
Expand Down
22 changes: 14 additions & 8 deletions src/m365/spo/commands/listitem/listitem-batch-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ interface CommandArgs {
}

interface Options extends GlobalOptions {
filePath: string;
webUrl: string;
filePath?: string;
csvContent?: string;
listId?: string;
listTitle?: string;
listUrl?: string;
Expand Down Expand Up @@ -58,6 +59,8 @@ class SpoListItemBatchAddCommand extends SpoCommand {
#initTelemetry(): void {
this.telemetry.push((args: CommandArgs) => {
Object.assign(this.telemetryProperties, {
filePath: typeof args.options.filePath !== 'undefined',
csvContent: typeof args.options.csvContent !== 'undefined',
listId: typeof args.options.listId !== 'undefined',
listTitle: typeof args.options.listTitle !== 'undefined',
listUrl: typeof args.options.listUrl !== 'undefined'
Expand All @@ -68,10 +71,13 @@ class SpoListItemBatchAddCommand extends SpoCommand {
#initOptions(): void {
this.options.unshift(
{
option: '-p, --filePath <filePath>'
option: '-u, --webUrl <webUrl>'
},
{
option: '-u, --webUrl <webUrl>'
option: '-p, --filePath [filePath]'
},
{
option: '-c, --csvContent [csvContent]'
},
{
option: '-l, --listId [listId]'
Expand All @@ -98,7 +104,7 @@ class SpoListItemBatchAddCommand extends SpoCommand {
return `${args.options.listId} in option listId is not a valid GUID`;
}

if (!fs.existsSync(args.options.filePath)) {
if (args.options.filePath && !fs.existsSync(args.options.filePath)) {
return `File with path ${args.options.filePath} does not exist`;
}

Expand All @@ -118,16 +124,16 @@ class SpoListItemBatchAddCommand extends SpoCommand {
}

#initOptionSets(): void {
this.optionSets.push({ options: ['listId', 'listTitle', 'listUrl'] });
this.optionSets.push({ options: ['listId', 'listTitle', 'listUrl'] }, { options: ['filePath', 'csvContent'] });
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
try {
if (this.verbose) {
await logger.logToStderr(`Starting to create batch items from csv at path ${args.options.filePath}`);
logger.logToStderr(`Starting to create batch items from csv ${args.options.filePath ? `at path ${args.options.filePath}` : `from content ${args.options.csvContent}`}`);
}
const csvContent = fs.readFileSync(args.options.filePath, 'utf8');
const jsonContent = formatting.parseCsvToJson(csvContent);
const csvContent = args.options.filePath ? fs.readFileSync(args.options.filePath, 'utf8') : args.options.csvContent;
const jsonContent = formatting.parseCsvToJson(csvContent!);
await this.addItemsAsBatch(jsonContent, args.options, logger);
}
catch (err: any) {
Expand Down

0 comments on commit 511394d

Please sign in to comment.